问题
I'm making a project for which I have a class online_score
which has as one of its attributes an array called url
of online_score_url
objects. What I did up to now is the following. The code might be a little dirty because I'm constantly trying new things, but as soon as I get it working, I'll clean it up.
views/online_score/new:
<h3>Links: </h3>
<div class="urlInput">
<%f.fields_for :url do |f| %>
<div class="inputset">
<%= f.label :url %> <%= f.url_field :url, :value => "http://www.google.be"%>
<%= f.label :description %> <%= f.text_field :description %>
<%= link_to_remove_fields "remove", f %>
</div>
<%end %>
<%= add_url_link "Add Another link", f %> #works fine if I remove this rule
</div>
views/online_score/_online_score_url_fields:
<%= f.fields_for :item do |b| %>
<div class="inputset">
<%= b.label :url %> <%= b.url_field :url, :value => "http://www.google.be"%>
<%= b.label :description %> <%#= b.text_field :description %>
<%= link_to_remove_fields "remove", b %>
</div>
<% end %>
My problem is now that I want to be able to dynamically add inputs for online_score_url
objects which I try to do with JQuery. I try to do this by rendering the partial like so:
helpers/online_scores_helper.rb:
def add_url_link(name, f)
link_to_function name do |page|
item = OnlineScoreUrl.new("", "")
instrument_online_score = render :partial => "online_score_url_fields", :locals => {:f => f, :item => item}
page << %{
$('.links').append("#{ escape_javascript online_score_url }");
}
end
end
The problem now is that it gives the following error:
ActionController::RoutingError in Online_scores#new
Showing /home/kvhooreb/jenna_vopro/score/app/views/online_scores/new.html.erb where line >#22 raised:
No route matches {:action=>"show", :controller=>"online_scores", :locale=>:en} Extracted source (around line #22):
19: 20: <%#= render "online_score_url_fields", :f => f %> 21: <%end %> 22: <%= add_url_link "Add Another link", f %> 23: 24:
25: Rails.root: /home/kvhooreb/jenna_vopro/scoreApplication Trace | Framework Trace | Full Trace app/helpers/online_scores_helper.rb:20:in
block in add_url_link' app/helpers/online_scores_helper.rb:15:in
add_url_link' app/views/online_scores/new.html.erb:22:inblock in >_app_views_online_scores_new_html_erb__1492671958001855269_22836400_2958786211230549542' app/views/online_scores/new.html.erb:3:in >
_app_views_online_scores_new_html_erb__1492671958001855269_22836400_2958786211230549542' RequestParameters:
{"piece"=>"1", "locale"=>"en"}
Now I have no clue what causes this. Does anyone have an idea? Thanks for your time.
回答1:
Based off the information you've provided, I am going to assume the problem is in app/helpers/online_scores_helper.rb
, specifically, this line:
$('.links').append("#{ escape_javascript online_score_url }");
If you've defined a standard resource for online_scores, you either are referring to a specific online score record online_score_url(online_score.id)
, or referring to the list of online scores online_score_urls
.
online_score_url
, being singular, normally would expect an id
in order to render the route. Without it, you would get the error message:
"No route matches {:action=>"show", :controller=>"online_scores", :locale=>:en} Extracted source (around line #22):"
来源:https://stackoverflow.com/questions/5685325/routing-error-in-rendering-partial-form