I have this form that I want to load via AJAX into the video show view:
<%= form_for #video, :url => {:action => "update"}, :remote => true do |f| %>
<div class="field">
<%= f.text_field :topic_names, :class => "topic_field" %>
</div>
<%= f.submit "Add Topic" %>
<% end %>
I have this link in the video show view that when clicked, I want it to render the form:
<%= link_to "Add Topics", new_topicable_path(#topicable, :format => :js), :remote => true, :id => 'edit_topics_link' %>
I put the form in topicables/new.html.erb and I have topicables/new.js.erb file which has:
$("<%= escape_javascript render(:file => 'topicables/new.html.erb') %>").insertAfter('.topic_holder');
$('#edit_topics_link').hide();
Now when I click the link, I get an error in my logs. This is what my logs show:
Started GET "/topicables/new.js" for 127.0.0.1 at Sat Apr 02 03:58:52 -0700 2011
Processing by TopicablesController#new as JS
Rendered topicables/new.html.erb (0.7ms)
Rendered topicables/new.js.erb (2.6ms)
Completed in 15ms
ActionView::Template::Error (undefined method `model_name' for NilClass:Class):
1: <%= form_for #video, :url => {:action => "update"}, :remote => true do |f| %>
2: <div class="field">
3: <%= f.text_field :topic_names, :class => "topic_field" %>
4: </div>
app/views/topicables/new.html.erb:1:in `_app_views_topicables_new_html_erb___1876131309_2172772700_0'
app/views/topicables/new.js.erb:1:in `_app_views_topicables_new_js_erb__1367015167_2172792200_0'
Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.0.4/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.1ms)
Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.0.4/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (92.5ms)
Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.0.4/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (98.9ms)
What's going on? How can I fix this error to make the AJAX rendering of the form work?
This is basically telling you that #video is not defined. If that's the case you can either define it in your new method or use :video which creates a new instance variable rather than looking for one in the page context. Then you can handle the find and update in the update method.
Edit: jQuery load method
$("#myButton").click(function(){
$("#target").load("/topicables/new #new_topicable");
});
Related
I want to put a simple email signup form in the footer of every page of my website. So I created a subscription scaffold and have made the following partial:
<%= form_for #subscription, :url => {:controller => 'subscriptions', :action => 'create'} do |f| %>
<div class="input-append">
<%= f.text_field(:email, :id => "appendedInputButton", :placeholder => 'Subscribe', :class => 'span4') %><button class="btn" type="submit" name="commit">Subscribe</button>
</div>
<% end %>
But despite specifying the controller and action I'm getting the following error:
undefined method `model_name' for NilClass:Class
The form works fine from the subscriptions/new page but how do I make it so I can submit the form from any controller and any page without having to define #subscription everywhere?
The simplest thing would be to simply change:
<%= form_for #subscription ...
To:
<%= form_for Subscription.new ...
And, like has already been noted, I doubt you need the url options.
When I change the content status on my application, background of database update works fine. But the link_to :loading operation is not working.
View : _feedback_question_status.html.erb for an element in page
<% if feedback_question.status == 1 %>
<span id="feedback_question">enabled -- <%= link_to "change", {:action=>"feedback_question_status",:status => 0,:feedback_question=>feedback_question.id}, :remote=>true, :loading => "$('#feedback_question_disable_#{feedback_question.id}').replaceWith('<p>Loading..</p>')", :update =>"feedback_question"%></span>
<% else %>
<span id="feedback_question">disabled -- <%= link_to "change",{:action=>"feedback_question_status",:status => 1, :feedback_question=>feedback_question.id }, :remote=>true, :loading => "$('#feedback_question_enable_#{feedback_question.id}').replaceWith('<p>Loading..</p>')", :update =>"feedback_question"%></span>
<% end %>
Controller:
has the update_attributes and render :partial=>'feedback_question_status'
And
on the Server:
Started GET "feedback/feedback_question_status?feedback_question=6&status=1" for 127.0.0.1 at 2012-08-27 16:25:47 +0530
... Snippet
(0.2ms) BEGIN
...
(30.7ms) COMMIT
Rendered _feedback_question_status.html.erb (2.7ms)
the last line shows the partial was renderd. but no changes on the views. It means there nothing happen to the element on the page.
rake routs for feedback_question:
feedback_feedback_question_status /feedback/feedback_question_status(.:format) feedback#feedback_question_status
feedback_questions feedback/new_feedback_question(.:format) feedback#new_feedback_question
edit_feedback_question /feedback/:id/edit_feedback_question(.:format) feedback#edit_feedback_question
update_feedback_question /update_feedback_question/:id(.:format) feedback#update_feedback_question
feedback_question_status /feedback_question_status(.:format) feedback#feedback_question_status
after refresh the page it shows the changes. but i don't want to load the page again.
You didn't passed the local variable "feedback_question" from controller to partial file _feedback_question_status.html.erb.
So you need to pass local variable "feedback_question" like below
render :partial => "feedback_question_status", :localts => { :feedback_question => #feedback_question }
where #feedback_question is a instance variable needs to be created in your controller.
Please replace this
<% if feedback_question.status == 1 %>
<span id="feedback_question">enabled -- <%= link_to "change", {:action=>"feedback_question_status",:status => 0,:feedback_question=>feedback_question.id}, :remote=>true, :loading => "$('#feedback_question_disable_#{feedback_question.id}').replaceWith('<p>Loading..</p>')", :update =>"feedback_question"%></span>
<% else %>
<span id="feedback_question">disabled -- <%= link_to "change",{:action=>"feedback_question_status",:status => 1, :feedback_question=>feedback_question.id }, :remote=>true, :loading => "$('#feedback_question_enable_#{feedback_question.id}').replaceWith('<p>Loading..</p>')", :update =>"feedback_question"%></span>
<% end %>
By this
<span id="feedback_question"><%= feedback_question.status == 1 ? "enabled" : "disabled" %> -- <%= link_to "change", {:action=>"feedback_question_status",:status => (feedback_question.status == 1 ? 0 : 1),:feedback_question=>feedback_question.id}, :remote=>true, :loading => "$('#feedback_question_disable_#{feedback_question.id}').replaceWith('<p>Loading..</p>')", :update =>"feedback_question"%></span>
Don't repeat your code.
I have a url with path /user_management/edit_official/:id
its corresponding output from rake routes is
user_management_update_official POST /user_management/edit_official/:id(.:format) {:controller=>"user_management/employees", :action=>"update_official"}
When I try to access url via form, url is not reached, because the url /user_management/edit_official/:id becomes a parameter.
Following is the log file entry?
Started POST "/user_management/edit_official/31" for 127.0.0.1 at
2012-01-02 11:05:21 +0530 Processing by ErrorsController#index as
HTML
Parameters: {"utf8"=>"✓",
"authenticity_token"=>"DED4E/9w/GDUQdjZ27mrUWrYBgipgHnNvS8mOjdaNXU=",
"employee"=>{"empl_id"=>"", "confirmation_date"=>"",
"designation_id"=>"", "rep_head1_id"=>"", "payment_mode"=>"",
"pf_number"=>"", "bank_name"=>"", "pt_applicable"=>"false",
"reg_date"=>"", "employee_type_id"=>"", "joining_date"=>"",
"rep_head2_id"=>"", "pf_applicable"=>"false",
"bank_account_number"=>"", "empl_email_id"=>"false",
"last_working_date"=>""}, "designation_level"=>"L-5b",
"user"=>{"username"=>"dsaf.adsfas", "password"=>"[FILTERED]"},
"commit"=>"Next", "a"=>"user_management/edit_official/31"}
Is there any reason for this. Thanks.
EDIT: Form included
<% url = user_management_update_official_path(#employee) %>
<%= form_for(#employee, :url => url, :html => { :enctype => 'multipart/form-data'} ) do |f| %>
<div id="employee_details" class="employee_form_steps">
<%= render :partial => 'user_management/employees/official_information',
:locals => { :f => f} %>
</div>
<div class="btn_row">
<%= content_tag(:button, '< Back', :id => 'official_information_back', :class => 'grey') %>
<%= f.submit 'Next', :class => 'green', :style => 'margin:0px;padding:4px;width:50px;' %>
<%= content_tag(:button, 'Cancel', :class => 'cancel grey') %>
</div>
<% end %>
The error must lie in your routes.rb
The rule in this file is: first match, first served.
Try to take the line corresponding to "errors#index" and put it under the one describing /user_management/edit_official/:id
The actual problem was the http method used for form. The form_for used put method while, in routes I specified POST. Changing POST to PUT, made everything work.
I have a single select_tag with categories gathered from array in controller. When the user selects a category I want the application to redirect to the selected category. I have the following code in my view which. (I've tried both using :method => :get and :post, only change is in development.log)
<%=select_tag "cat_selected", options_for_select(#cats_for_mt)%><br>
<%=observe_field 'cat_selected',
:url => {:action => :viewflokkur},
:with => 'cat',
:method => :get %>
When I select one of the options the following gets logged to development.log.
Processing CategoriesController#viewflokkur (for 127.0.0.1 at 2010-06-12 12:33:26) [GET]
Parameters: {"cat"=>"Taugasjúkraþjálfun", "authenticity_token"=> "B2u5ULNr7IJ/ta0+hiAMBjmjEtTtc/yMAQQvSxFn2d0="}
Rendering template within layouts/main
Rendering categories/viewflokkur
Completed in 20ms (View: 18, DB: 0) | 200 OK [http://localhost/categories/viewflokkur?cat=Taugasj%C3%BAkra%C3%BEj%C3%A1lfun&authenticity_token=B2u5ULNr7IJ%2Fta0%2BhiAMBjmjEtTtc%2FyMAQQvSxFn2d0%3D]
According to this I should now be in "viewflokkur", but nothing changes in the browser window. Is there anything else I need to do, maybe in the controller?
BR,
Sindri
here is an example of Observe field :
<label for="search">Search term:</label>
<%= text_field_tag :search %>
<%= observe_field(:search,
:frequency => 0.5,
:update => :results,
:url => { :action => :search }) %>
<div id="results"></div>
Found here.
This is all new territory for me, but I am working through a Rails book that was written before start_form_tag was deprecated and I am running into problems with the books example code using remote_form_tag. I have the other forms working but can't get this one up and running. Here's the code:
<h1>Categories</h1>
<ul id="category_list">
<%= render :partial => 'category', :collection => #categories %>
</ul>
<br/>
<p id="add_link"><%= link_to_function("Add a category", "Element.remove('add_link');
Element.show('add_category')") %></p>
<div id="add_category" style="display:none;">
<%= form_remote_tag(:url => {:action => 'new'},
:update => "category_list",
:position => :bottom,
:html => {:id => 'category_form'}) %>
Name: <%= text_field "category", "name" %>
<%= submit_tag 'Add' %>
<%= end_form_tag %>
</div>
This is exactly how it appears in the book, and doesn't compile. I've tried changing to match the form_tag blocks but the form enclosed by the "add_category" div never shows up.
Thanks!
UPDATE: Just found that it doesn't appear that the prototype script is not getting loaded.
both the remove and show methods on Element are showing up as undefined in Firebug. I am not sure why it is not showing up though.
I left out the = sign when including the javascript libraries in standard.html
Line should be this:
<%= javascript_include_tag :defaults %>
Vote to close this question?