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
Related
I have a Job model with a first_booking_time attribute. The datetime_local_field form helper doesn't pre fill the attribute stored on the Job instance.
# renders empty field even though job.first_booking_time is set
<%= form_for #job do |f| %>
<%= f.datetime_local_field :first_booking_time %>
<% end %>
However, if I use the datetime_local_field_tag helper and pass in the value, it works:
# pre fills what's in job.first_booking_time
<%= form_for #job do |f| %>
<%= datetime_local_field_tag 'job[first_booking_time]', #job.first_booking_time %>
<% end %>
How can I use the first syntax with the helper pre filling?
Try the below code
<%= f.datetime_local_field :first_booking_time , :value => #job.first_booking_time %>
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?
Okay so I am quite new to Rails and am trying to do the following without success:
I have an Object (from my Active Record) containing a project, which contains n sub-projects, which contain n tasks. Now for each of these I want a partial view.
So I render from the project view the sub-project with the following code:
<%= render(:partial => 'subproject', :collection => #project.sub_projects) %>
Within my sub-project partial view called _subproject.rhtml (adding the code to a good ol Rails 1.2.3 project), so I can access the data like this:
<%= subproject.name %>
That will print out the name alright but when I try to generate a textfield this won't work:
<%= text_field 'subproject', 'name' %>
But this will:
<%= text_field 'subproject', 'name', :value => subproject.name %>
What am I doing wrong?
Edit: Changed title due to my problem is not passing the value but displaying it within a form field.
Edit2: As requested my controller code:
#project = Project.find(params[:id])
You can write this:
<%= render(:partial => 'subproject', :collection => #project.sub_projects) %>
as
<%= render :partial => #project.sub_projects %>
This will render every sub project with the sub_projects/_sub_project.html.erb partial. A little shortcut.
This:
<%= text_field 'subproject', 'name' %>
Says create a text_field called: subproject[name], but doesn't give it a value. You need to pass the value you want to set (the code that works).
The more idiomatic way of doing this now is with form_for:
<% form_for #subproject do |f| %>
<%= f.text_field :name %>
<% end %>
Or if you're using formtastic (https://github.com/justinfrench/formtastic), which is fantastic, you'd write:
<% semantic_form_for #subproject do |f| %>
<%= f.input :name %>
<% end %>
I hope this helps!
I am stuck one more time ... and one more time I suspect it's a stupid syntax problem:
I want to pass 2 vaiables in the url with my super simple search form.
I was expecting a URL like this:
http://mydomain/categories/search?search=pdf&os=2
But I get this:
http://mydomain/categories/search?search=pdf&os[]=
I thought it should work like this:
<% form_tag search_path, :method => 'get' do %>
<%= text_field_tag :search, params[:search] %>
<%= hidden_field :os, params[#category.id] %>
<%= submit_tag "Search", :name => nil %>
<% end %>
... but well, it didn't do it ...
Does anyone know where I am going wrong?
Thanks!
Val
You need to modify the line a bit, using hidden_field_tag:
<%= hidden_field_tag :os, :value => #category.id %>
See the hidden_field_tag documentation for more information.
<%= hidden_field :os, params[#category.id] %>
Is going to access a key in the params hash with #category.id, is there such a key? Looks like not, as its returning nil.
Seems like you want something to the effect of
<%= hidden_field :os, #category.id %>
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) %>