I have a problem I can not resolve on a form
Here's my view:
<h1>create manager </h1>
<% form_tag :action => 'create_manager' do %>
<%= text_area :user, :nom %><br/>
<%= date_select :user, :date_embauche %>
<%= submit_tag "Submit" %>
<% end %>
and here is my controller:
def create_manager
tmp = params[:nom]
p(tmp)
render :partial => "adminpartial"
end
The problème is that params[:nom] return alltime nil.
I think i'm not using correctly the params variable.
Does anyone have an idea about this?
Did you look at the params passed in your logs?
I guess you might find something in params[:user][:nom]as explained here.
BTW, did you carefully read this?
Related
I am trying to generate dynamic CSS ids for javascript purposes, and the following code is giving me an error:
<%= form_for #item, html: { multipart: true } do |f| %>
<%= f.fields_for :item_images do |builder| %>
<%= image_tag builder.object.image.url(:large), :class => "cropbox", 'data-id' => builder.object.id %>
<% for attribute in [:crop_x, :crop_y, :crop_w, :crop_h] %>
<%= builder.text_field attribute, :id => attribute + builder.object.id %>
<% end %>
<% end %>
<% end %>
I know I'm not concatenating attribute and builder.object.id properly, but I've tried everything with no luck. I get this error message:
undefined method `+' for :crop_x:Symbol
Appreciate any help, thanks!
What is your expected result?
Can you p out what '''attribute''' is within your expression?
Also, have you tried .concat instead of '+'?
Away from my desk, but my suggestion is something like:
Example:
attribute.concat(builder.object.id)
Maybe need to convert attribute.to_i or .to_s into a responsive type or create a different dynamic CSS id solution
I am atempting to dinamically create form elements given a certain AJAX request.
This is my setup:
View:
<%= link_to 'Next', check_unique_id_students_path, :remote => true %>
<div id="guardian_student_details"></div>
Controller:
def check_unique_id
#student = Student.new
#this_form = ActionView::Helpers::FormBuilder.new(:student, #student, #template, {}, proc{})
end
JS:
jQuery("#guardian_student_details").html("<%=escape_javascript(render :partial => "student_details", :locals => { :s => #this_form }) %>");
Partial:
<% puts s.text_field :first_name %>
<% puts s.field_helpers %>
For debugging purposes i placed the following lines at the very beginning of my partial:
<% puts s.class.to_s %>
<% puts s.object.to_s %>
This prints out :
ActionView::Helpers::FormBuilder
Student
This should work. However rails is giving the following error:
ActionView::Template::Error (undefined method `text_field' for nil:NilClass):
1: <% puts s.class.to_s %>
2: <p>
3: <%= s.text_field :first_name, :class => 'text_input is_empty' %>
4: <%= s.label :first_name %><strong>*</strong>
5: </p>
6:
app/views/students/_student_details.html.erb:3:in _app_views_students__student_details_html_erb__2485891544130782916_2214680440'
app/views/students/check_unique_id.js.erb:2:in_app_views_students_check_unique_id_js_erb__3504800328150418937_2214933160'
Which implies that "s" is NIL something I verified just 2 lines before. Does anybody have any ideas? i dont know if this has something to do with the "#template" variable initialized in the controller. Which i played around with and accepts practically anything and if printed is nil.
Any help would be appreciated. Thanks
Final note:
I tried to implement this: AJAX update of accepts_nested_attributes_for partials
For anyone needing to build a form builder in the controller, view_context still works there. Using Rails 4.1.4:
#object = Object.new
#f = ActionView::Helpers::FormBuilder.new(:object, #object, view_context, {})
In the view, I've found that 'view_context' does not work in Rails 3.1. Instead try 'self' when creating a FormBuilder object.
s = ActionView::Helpers::FormBuilder.new(:student, #student, self, {}, proc{})
Try this in a console :
s = ActionView::Helpers::FormBuilder.new(:student, #student, #template, {}, proc{})
s.text_field :first_name
You will have the same error. I think the problem come from your creation of the form_builder object, even if I don't know the exact mistake...
Your solution seems to me to be a little much complex. You can try this solution :
#html.erb
<% form_for #student do |f| %>
<div id='guardian_student_details' class='hide-this-block'>
<%= render :partial => "student_details", :locals => { :s => f }) %>
</div>
<% end %>
#js
jQuery("#guardian_student_details").show();
Generally, I prefer keep javascript and ruby separated.
I just started to learn rails. My rails version is 3.0.7. I am wondering what are the differences between <% form_for :project_profile %> and <% form_for #project_profile %>. I have this question because I went into the following situation:
If I use <% form_for :project_profile %>, it doesn't give me an error, but the form is actually not working.
If I use <% form_for #project_profile %>, I will get an error: undefined method `project_profile_path' for #<#:0x00000103546d80>
If I use <%= form_for #project_profile, :url => "/projects/#{params[:project_id]}/profile/update" do |f| %>, it will work but the code is ugly.
You can refer to the following codes to understand the context of my problem better.
I have a project model and a project_profile model. One project has one project_profile.
The following two lines are from my routes.rb.
match '/projects/:project_id/profile/edit' => "project_profiles#edit"
match '/projects/:project_id/profile/update' => "project_profiles#update"
This is from my project_profiles_controller.rb
class ProjectProfilesController < ApplicationController
def edit
#project_profile = Project.find(params[:project_id]).project_profile
end
def update
#project_profile = Project.find(params[:project_id]).project_profile
respond_to do |format|
if #project_profile.update_attributes(params[:project_profile])
format.html {}
else
format.html { render :action => "edit" }
end
end
end
end
The following code is from _form.html.erb
<%= form_for #project_profile, :url => "/projects/#{params[:project_id]}/profile/update" do |f| %>
<div class="field">
<%= f.label :title %>
<br/>
<%= f.text_field :title %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
You should learn about resource and nested resource routing in Rails.
The way you define controller is also not conventional. There is an article on Rails Guides on Getting Started section that covers that too.
Basically spoken, form_for #project_profile is an advanced (resource-oriented), nowadays preferred style. If you want to dig a little deeper into this, the API itself explains the difference pretty well.
http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for
cheers
I have been struggling with a problem in Rails for a couple of days and still could not find the solution. Could you help me with that?
Problem: I have a search box that puts a :search_string entry in the params structure. I use a form_tag for that and it works fine.
<% form_tag :controller=> 'items', :action => 'find' do %>
<%= text_field_tag :search_string, params[:search_string] %>
<% end %>
The problem is when I want to add and update other params key-value (in another view), for instance :start_date, to filter the search_string result. Here is the code snipped that I use in the view:
<% form_tag :controller=> "items", :action => "find", :params => params do %>
<%= hidden_field_tag :date_start, '2010-04-01' %>
<%= submit_tag 'April' %>
<% end %>
<% form_tag :controller=> "items", :action => "find", :params => params do %>
<%= hidden_field_tag :date_start, '2010-03-01' %>
<%= submit_tag 'March' %>
<% end %>
When I first click on "April" submit button, then the params is correctly passed to the controller (i.e. there is a params[:start_date]='April'). However when I try to click "March" button afterwards, the params[:start_date] is not updated. I definitely think this is a stupid newbie mistake, but I cannot figure out how to properly use the form_tag. Could you tell me if I am doing something work? Otherwise, could you advise me which is the best way to update the params using form_tag's ? Thank you very much in advance.
Miquel
What you may want to do is instead force-merge the parameters, something along the lines of:
<% form_tag :controller=> "items", :action => "find", :params => params.merge(:date_start => '2010-03-01') do %>
<%= submit_tag 'March' %>
<% end %>
There is a chance you're inadvertently submitting two of the same parameter and the first of them is getting picked, but since the "first" is not clearly defined, you may get inconsistent results.
Have a look in your log file to see what parameters are received from the two forms.
One of the things I'm doing includes several links on the show view. For instance, I have a link (or button) for "Accepting", and another one for "Rejecting". Click on Accept, and the model updates the is_accepted field as true, click on Reject, and the is_accepted field is false.
Now, how best do I handle this? In ASP.NET, I would have simply created a LinkButton and written a handler, but Rails doesn't work that way, so I'm trying to figure out how to essentially replicate what a LinkButton would do.
Right now, I'm coding two forms on the same view, nearly identical, that look like this:
<%= form_for #thing do |f| %>
<%= hidden_field_tag 'thing[is_accepted]', '1' %>
<%= f.submit "Accept" %>
<% end %>
<%= form_for #thing do |f| %>
<%= hidden_field_tag 'thing[is_accepted]', '0' %>
<%= f.submit "Reject" %>
<% end %>
This feels weird to me, but I can't seem to find anything that says this is the wrong way to do it.
I could, I assume, dry things up by using a partial and/or a helper method, but I wanted to make sure I'm on the right track and not doing something totally wrongly.
You can give your submit tag a name.. ie
<%= form_for #thing do |f| %>
<%= hidden_field_tag 'thing[is_accepted]' %>
<%= f.submit "Accept", :name => 'accept' %>
<%= f.submit "Reject", :name => 'reject' %>
<% end %>
Then you can detect the name in params[] and skip the '1'/'0' value.
I think you're going about it the right way. One way to clean up your forms is by using the model form helpers all the way through, so you'd end up with something like
<%= form_for #thing do |f| %>
<%= f.hidden_field :accepted, :value => true %>
<%= f.submit "Accept" %>
<% end %>
<%= form_for #thing do |f| %>
<%= f.hidden_field :accepted, :value => false %>
<%= f.submit "Reject" %>
<% end %>
But other than that, it looks like the right way to go about it. I would suggest against creating new methods to do this, because you're not doing anything outside of normal web requests (updating a model in this instance).
Using the submit tag as the switch and detecting it in params[] is also a good way, but I usually prefer to keep my controllers as vanilla as possible. In the end, both of these ways would end up with the same amount of 'stuff' in the UI, so whichever style you'd rather use should be fine.
Depending on how you want your UI to work you might consider link_to_remote (part of the prototype helper) - you can specify an action, params etc, and have it return some JS that gets run.
If you're using map.resources in your routes.rb you should be able to do something like this:
map.resources :things, :member => {:accept => :get, :reject => :get}
Then in your controller:
def accept
#thing = Thing.find(params[:id])
#thing.is_accepted = true
#thing.save
end
def reject
#thing = Thing.find(params[:id])
#thing.is_accepted = false
#thing.save
end
And finally in your view:
<%= link_to 'Accept', accept_thing_url(#thing) %>
<%= link_to 'Reject', reject_thing_url(#thing) %>
Or if you are using Ajax:
<%= link_to_remote 'Accept', :url => accept_thing_url(#thing) %>
<%= link_to_remote 'Reject', :url => reject_thing_url(#thing) %>