HAML form_tag problems with dropdown menue - haml

%h2 Your "followers":
- form_tag twitter_path do |f|
= f.select{:name => "dropdown"}
- for follower in #followers
%option{:value => follower['id']}= h follower['name']
= f.submit_tag "Who leaves comments?"
How do you properly format this HAML? Its returning a Syntax Error.

You're using HAML syntaxe inside Ruby code here:
= f.select{:name => "dropdown"}
The brace { is interpreted as the begining of a Ruby block (like in array.map { ... }), because everything after the = or - prefix in HAML is evaluated as Ruby code.
Also, you're using the form_for syntax while using the form_tag method (see this question).
The form_tag method doesn't provide a form object f.
You should use the select_tag method from FormTagHelper instead, along with a FormOptionsHelper method:
- form_tag twitter_path do
= select_tag "dropdown", options_from_collection_for_select(#followers, "id", "name")
= submit_tag "Who leaves comments?"

Related

Why doesn't my partial work with slim and Rails 3.2?

I'm using Rails 3.2 and instead of ERB, I am using slim.
Here is my form partial:
= simple_form_for #game, :html => { :class => 'form-horizontal' } do |f|
The syntax error I am getting is:
_form.html.slim:1: syntax error, unexpected ')' ... => 'form-horizontal' } do |f|))).to_s));
But I don't know where it expects to put the ')'?
Thanks
The syntax should be:
= form_for([#resource], html: { multipart: true }, remote: true) do |f|
Try substituting your values.
I was not able to find the correct documentation for slim form helper.

Rails simple_form pre-populated input values lost during server side form validation [duplicate]

This question already has an answer here:
Rails simple_form server side validations lost URL params
(1 answer)
Closed 8 years ago.
I have a simple_form in my rails app that I want to pre-populate
some of the fields with URL parameters.
Example URL is:
http://localhost:3000/surveys/new?phone=8675309
This pre-populate the phone field corretly using this code:
<%= simple_form_for #survey, :html => { :class => 'form-horizontal' } do |f| %>
<%= f.input :state, :required => true %>
<%= f.input :zip, :required => true %>
<%= f.input :phone, :required => true, :input_html => { :value => params['phone'] } %>
<% end %>
The problem is if the form is submitted without a required field
the form is reloaded but the phone input value is not retained.
If the form is submitted without a value for zip
the reloaded page with validation errors in red URL is now:
http://localhost:3000/surveys
If, say, the state field is correct but no zip code the form is reloaded
with an error msg saying zip is required.
The state value is retained however the phone is not.
I guess it has to do with :value => params['phone'] in the f.input for the phone.
Is there anyway I can populate the simple_form with URL paramenters
on it's initial load and have those value retained if the serverside validation fails?
Many thanks.
Remove the :value from your view:
<%= f.input :phone, :required => true %>
and use this URL:
http://localhost:3000/surveys/new?survey[phone]=8675309
That should generate the "standard" survey parameter hash expected by the controller. In my test, that works the same as if the user had typed in the value, with the usual validation handling.
Inside the controller, the hash is called params[survey] with individual parameters represented as params[survey][phone], params[survey][zip], etc.
With the help of this answer, I found that you can generate the URL with the link_to signature link_to(body, url_options = {}, html_options = {}):
<%= link_to 'New survey', { :controller => "surveys",
:action => "new",
:survey => { :phone => "8675309", :zip => "10001" } },
{ :target => "_blank" } %>
Note that url_options is the first hash, and within that hash you have a survey hash for pre-loading values. Finally comes the optional hash of html_options (where I added target="_blank" for illustration purposes).
Thanks Mark,
I re-posted this question again a while back and it was answered corretly here:
Rails simple_form server side validations lost URL params

Rails 3.1, passing parameters for colection_select from controller

I have this collection_select usage in my view:
<%= collection_select(:production_year, :id, #car_models, :id, :name, { :prompt => "Year" }, { :disabled => "disabled" } ) %>
But it seems, that i'll add much logic for this select box. So i want pass parameters for this collection_select from my controller. How can i do this?
Was trying to pass array with parameters, but got many errors. Pls show correct way for this.
In your controller: #collection_select_params = [ ... ]
And in your view: <%= collection_select(*#collection_select_params) %>
The * prefix will indicate to ruby that this array is to be passed as an args list.

assigning parameters to a model in a link_to helper

if am trying to pass a parameter through a link and assign it to a specific model. here is the code i'm working with:
<%= link_to ( image_tag("item.png", :size => "50*50", :border=> 0, :alt => "item", :title => "item"), {:action => 'initialize_order', :frame_id => 1 }) %>
right now :frame_id is getting passed through as:
{"frame_id"=>"1"}
i want the parameter to be assigned to the model :order, returning:
"order"=>{"frame_id"=>"1"}
i know the answer must be simple but i've searched for a while now.
try this in your url args:
{:action => 'initialize_order', :order => {:frame_id => 1 }}
alternatively, since you only have one parameter, just pass it as :id and assign it in the controller.
Even better, build your app using RESTful routes, with an orders_controller and an initialize method. If orders and frames have a parent-child relationship, then nest them in your resources. That way you can use all the path generators.

Rails 3 - How to send data on link_to :remote=>true?

I am trying to figure out the new link_to in Rails 3 but i still don't get it
In Rails 2 I do:
<%= link_to_remote "My Link",:url=>{:action=>:myaction},:with=>"'data='+$('#someField').attr('value')" %>
but with the new syntax in Rails 3 how should it be?
I'm trying something like
<%=link_to "My Link",{:action=>"myaction"},:with=>"'data='+$('#someField').attr('value');",:remote=>true%>
but I'm not getting the quantity params in the controller's action
Something like this will send a "data" parameter with the value = 10.
link_to "My Link", { :controller => 'myctrler', :action=>"myact", :data=> 10 }, :remote=>true
I've never seen/used the :with option before. I'm sorry I can't help on that one.
They say its no longer supported in this answer on How to send Javascript Variable to a controller's action with the link_to helper?
you just need to add the variable with the value in the path url, for example:
<%= link_to "SEND DATA", "server_function?myData=10", remote: true %>
and if you need to send more than one parameter you must to use & for example ?myData=10&myOtherData=12, where the params are myData with the value of 10 and myOtherData with 12.