2017-05-06 5 views
0

Wenn ich link_to in Schienenfalsche URL in dem link_to Helfer

Ich weiß, dass die Form

link_to "Profile", profile_path(@profile) 
# => <a href="/profiles/1">Profile</a> 

ist aber in meinem Code

<% @posts.each do |post|%> 
    <h2><%=post.title %></h2> 
    <p><%= post.content%></p> 
    <%=link_to "show", posts_path(post.id) %> 
<%end%> 

ich meine url erwarten sieht aus wie posts/1 aber es war posts.1

+0

können Sie versuchen, '<% = link_to "show", posts_path (post)%>' –

+0

leider nicht :-( –

+0

funktioniert kann Sie teilen Ihre routes.rb-Datei, Sie müssen post_path (Beitrag) in Ihrer HTML-Datei hinzufügen – tjs7706

Antwort

0

Versuchen Sie <%=link_to "show", post_path(post) %>

post nicht posts. Siehe hier http://guides.rubyonrails.org/routing.html

+0

OMG ... Vielen Dank ... –

+0

Das wäre gut, wenn Sie das Problem und Ihre Antwort beschreiben können, um mehr Upvotes zu sammeln . –

0

link_to kommt mit Syntax/Signatur link_to(name = nil, options = nil, html_options = nil, &block) Das erstellt ein Link-Tag des angegebenen Namens mit einer URL, die durch den Satz von Optionen erstellt wurde.

Signaturen;

link_to(body, url, html_options = {}) 
    # url is a String; you can use URL helpers like 
    # posts_path 

link_to(body, url_options = {}, html_options = {}) 
    # url_options, except :method, is passed to url_for 

link_to(options = {}, html_options = {}) do 
    # name 
end 

link_to(url, html_options = {}) do 
    # name 
end 

Ich werde das gleiche Beispiel aus Ihrer Frage nehmen,

Dieser link_to "Profile", profile_path(@profile) schafft Pfad;

# => <a href="/profiles/1">Profile</a> 

während <%=link_to "show", posts_path(post.id) %> schafft

# => <a href="/profiles.1">show</a> 

Weitere Möglichkeiten entsprechenden Routen zu erstellen sind wie folgt;

link_to "Profile", @profile 
# => <a href="/profiles/1">Profile</a> 

link_to "Profile", controller: "profiles", action: "show", id: @profile 
# => <a href="/profiles/show/1">Profile</a> 

Hoffnung könnte dies helfen Ihnen sonst das sehen link_to apidoc

Verwandte Themen