I have a form that is non-responsive for some reason and I'm stuck!
I have followed the RailsCast here but it's based on an older version of rails.
Here's my view code, pretty sure that's where my trouble is:
<%= form_tag publish_selected_posts_path do %>
<% #posts_inactive.each do |post| %>
<tr>
<td><%= check_box_tag "post_ids[]", post.id %></td>
<td><%= link_to post.title, post_path(post) %></td>
<td><%= post.created_at.to_s(:short) %></td>
<td><%= post.user.email %></td>
<td><%= post.user.email %></td>
<td><%= post.state.name %></td>
<td><%= post.city.name %></td>
<td><%= post.expire_date.to_s(:short) %></td>
<td>
<div>
<div class="dark" style="float:left;">
<%= link_to 'Edit', edit_post_path(post), :class => 'btn btn-mini dark' %>
</div>
<div style="float:left;">
<%= button_to 'Delete', post_path(post), :method => :delete, :confirm => 'Are you sure?', :class => 'btn btn-mini btn-danger' %>
</div>
<div class="dark" style="float:left;">
<%= link_to publish_link_text(post),
toggle_publish_post_path(post),
:class => 'btn btn-mini dark' %>
</div>
<div style="clear:both;">
</div>
</div>
</td>
</tr>
<% end %>
</tbody>
</table>
<%= submit_tag "Publish Selected", :class => 'btn btn-mini dark' %>
<% end %>
When I press the submit button, nothing is happening... As best I can tell, everything is as it should be based on the rails cast, but it's just not working.
Here's the HTML this form outputs:
<form accept-charset="UTF-8" action="/dashboard/posts" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓" /><input name="authenticity_token" type="hidden" value="pwTuSXar01OT26FHf1bMyxKsSQ7MHWn/u3BQ9PmffS4=" /></div>
<tr>
<td><input id="post_ids_" name="post_ids[]" type="checkbox" value="29" /></td>
<td>Another Post</td>
<td>30 Nov 05:03</td>
<td>guitarjoe02#email.com</td>
<td>guitarjoe02#email.com</td>
<td>Alaska</td>
<td>Anchorage</td>
<td>28 Jan</td>
<td>
<div>
<div class="dark" style="float:left;">
Edit
</div>
<div style="float:left;">
<form action="/posts/29" class="button_to" method="post"><div><input name="_method" type="hidden" value="delete" /><input class="btn btn-mini btn-danger" data-confirm="Are you sure?" type="submit" value="Delete" /><input name="authenticity_token" type="hidden" value="pwTuSXar01OT26FHf1bMyxKsSQ7MHWn/u3BQ9PmffS4=" /></div></form>
</div>
<div class="dark" style="float:left;">
Publish
</div>
<div style="clear:both;">
</div>
</div>
</td>
</tr>
<tr>
<td><input id="post_ids_" name="post_ids[]" type="checkbox" value="28" /></td>
<td>New Post...</td>
<td>16 Nov 01:09</td>
<td>guitarjoe02#email.com</td>
<td>guitarjoe02#email.com</td>
<td>Alaska</td>
<td>Anchorage</td>
<td>14 Jan</td>
<td>
<div>
<div class="dark" style="float:left;">
Edit
</div>
<div style="float:left;">
<form action="/posts/28" class="button_to" method="post"><div><input name="_method" type="hidden" value="delete" /><input class="btn btn-mini btn-danger" data-confirm="Are you sure?" type="submit" value="Delete" /><input name="authenticity_token" type="hidden" value="pwTuSXar01OT26FHf1bMyxKsSQ7MHWn/u3BQ9PmffS4=" /></div></form>
</div>
<div class="dark" style="float:left;">
Publish
</div>
<div style="clear:both;">
</div>
</div>
</td>
</tr>
</tbody>
</table>
<input name="commit" type="submit" value="Save changes" />
</form>
Any ideas?
Ah ha! The culprit is your button_to calls. Check out the documentation for button_to. As you will see, that helper wraps itself in a mini HTML form. In your case, its creating a form within a form, which is invalid.
You should be able to do the exact same action using link_to instead. So your button_to will end up looking like this:
<%= link_to 'Delete', post_path(post), :method => :delete, :confirm => 'Are you sure?', :class => 'btn btn-mini btn-danger' %>
Try that and let me know if it makes a difference.
Your submit tag doesn't seem to be inside the form. That will make it not actually submit the form.
Edit: I think I might be wrong... but the dodgy indentation of your code makes it hard to tell. Can you clean up the indentation to make do/ends (and the code they contain) clearer and easy to see at a glance?
Related
Nested form with radio buttons
Hi there,
I am a Rails newbie .
I've been trying to build a nested form with radio buttons.
Let me explain it.
The form for choosing a staff from a list using a radio button
Let's say
O John
O Jake
O Blake
O All
and then choosing another a radio button option for multiple amount choices such as
O 2$
O 4$
O 8$
O 10$
Then finally a submit button to post the values to the paymentgate Api.
For example, John, 8$
<% #staffs.each do |staff| %>
<tr>
<td><%= staff.first_name %></td>
<td><%= staff.last_name %></td>
<td><input id="favorite_color_maroon" name="favorite_color" type="radio" value="maroon" /></td>
<input type="radio" name="picker" value="whatever_who_cares"> Whatever, who cares? </form>
<td><%= link_to 'Show', staff %></td>
<td><%= link_to 'Edit', edit_staff_path(staff) %></td>
<td><%= link_to 'Destroy', staff, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
<form action="/action_page.php">
<p>Please select your age:</p>
<input type="radio" id="age1" name="age" value="2">
<label for="staff_value">2</label><br>
<input type="radio" id="age2" name="age" value="4">
<label for="age2">4</label><br>
<input type="radio" id="age3" name="age" value="8">
<label for="age3">8</label><br><br>
<input type="radio" id="age4" name="age" value="10">
<label for="age4">10</label><br><br>
<input type="submit" value="Submit">
</form>
so how can I associate those radio buttons along with submit button to the external api?
I am creating a rail comment system. I managed to create the comic with the comments. I manage to answer the first comment but when I want to answer the other comments the answer stores it in bdd but does not appear
Here is the display code
messages.each do |message| %>
<p class="msg">
<b class ="name"> <%= message.Name %></b>
<%= message.Text %></p>
Répondre
<% msgs = Msg.where(msgable_id: message.id, project_id: params[:id]) %>
<p class="answer">
<% msgs.each do |msg|%>
<b class="name"><%= msg.Name %></b>
<%= msg.Text %></br></p>
Répondre
<% end %>
<form action="/reply/" method="POST" style="width:20vw;">
<textarea rows="4" cols="50" name="Texte" style="width:20vw;height: 2vw;margin-top:1vw;"></textarea>
<input type="hidden" name="id" value="<%= message.id %>" />
<input type="hidden" name="id_project" value="<%= message.id %>" />
<button type="submit" style="margin-bottom:1vw;">Envoyer</button>
</form>
<%
end %>
<% end %>
Normally, in order to get a date select in rails, I write this:
<%= form_for(:buddy, :url => {:action => :create}) do |f| %>
<%= f.date_select(:dob) %>
<%= f.submit("HI") %>
<% end %>
and get this html as a result:
<form action="/buddy/create" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="✓" /><input type="hidden" name="authenticity_token" value="Qs8RK+6gEt9ZoycMbMHwX4xMyoFpRvjA/El3cLnO30RXddr864WmEXzyAgYtXBe5Y9l0zC3UDw4ggRw1xYW1sg==" />
<select id="buddy_dob_1i" name="buddy[dob(1i)]">
<option value="2010">2010</option>
<!-- ETC -->
<select id="buddy_dob_2i" name="buddy[dob(2i)]">
<option value="1" selected="selected">January</option>
<!-- ETC -->
<select id="buddy_dob_3i" name="buddy[dob(3i)]">
<option value="1">1</option>
<!-- ETC -->
<input type="submit" name="commit" value="HI" />
But now I'm using bootstrap. If I use the bootstrap_form_for gem, I run into the problem that on small screens, the three selects get placed on top of each other, which is unacceptable. So I came up with what I thought was a good solution: to place each of the selects in a different field of a table, which prevents the behaviour. So I have written this code:
<%= form_for(:buddy, {:action => :create, :role => 'form'}) do |f| %>
<div class="form-group">
<label for="buddy_dob">Date of Birth</label>
<table class="table functional-table">
<tr>
<td>
<%= select_year(Date.today, {:start_year => DateTime.now.year, :end_year => DateTime.now.year - 117}, {:name => 'buddy[dob(1i)]', :id => 'buddy_dob_1i', :class => 'form-control'}) %>
</td>
<td>
<%= select_month(Date.today, {}, {:name => 'buddy[dob(2i)]', :id => 'buddy_dob_2i', :class => 'form-control'}) %>
</td>
<td>
<%= select_day(1, {}, {:name => 'buddy[dob(3i)]', :id => 'buddy_dob_3i', :class => 'form-control'}) %>
</td>
</tr>
</table>
</div>
<div class="form-group">
<%= f.submit("HI") %>
</div>
<% end %>
which produces this result:
<form role="form" action="/buddy/new" accept-charset="UTF-8" method="post"><input name="utf8" type="hidden" value="✓" /><input type="hidden" name="authenticity_token" value="6Ek01J6PIgzHl/NLpjI6V+kolUVCpw7jrQTF8eYT3ef98/8Dm6qWwuLG1kHnr92xBr0rCAY1+S1xzK60mli3EQ==" />
<div class="form-group">
<label for="buddy_dob">Date of Birth</label>
<table class="table functional-table">
<tr>
<td>
<select id="buddy_dob_1i" name="buddy[dob(1i)]" class="form-control">
<option value="2015" selected="selected">2015</option>
<!-- ETC -->
</td>
</tr>
</table>
<table class="table functional-table">
<tr>
<td>
<select id="buddy_dob_2i" name="buddy[dob(2i)]" class="form-control">
<option value="1" selected="selected">January</option>
<!-- ETC -->
</td>
</tr>
</table>
<table class="table functional-table">
<tr>
<td>
<select id="buddy_dob_3i" name="buddy[dob(3i)]" class="form-control">
<option value="1" selected="selected">1</option>
<!-- ETC -->
</td>
</tr>
</table>
<div class="form-group">
<input type="submit" name="commit" value="HI" />
</div>
</form>
The display for this works exactly as I want, however when I try to submit the form I am redirected to the action :new with no errors registered by rails and no entry created. I never reach :create. I suspect this might be because I've manually separated the date fields which date_select separates and then pieces back together and rails cannot handle this, but I am not sure. How can I fix this and get the form working?
You have nested forms - this is illegal! Most of the browsers will simply ignore the inner form tag, so you'll end up with a DOM structure as presented. The outer form does not specify the action attribute, so browsers will append the current path as an action, in this case - /buddy/new. I'm a little bit surprised you're not getting routing error here - did you by any chance use match /buddy/new without specifying the verb?
Anyway - the solution is to get rid of surrounding form (the one with the role) and instead attach this role to your form_for call:
<%= form_for(:buddy, url: {action: :create}, html: {role: :form) do |f| %>
SOLVED! I just changed form_for to form_tag and it now works as expected. Thanks to BroiSatse for the information about ActionDispatch knowing how to convert the params; it encouraged me to look elsewhere for problems.
Your select names are not "standard". buddy[dob(3i)] is not a valid name.
When the query is sent to Rails, a first module changes all the received names into params. Here, it can't, and fail.
You should set somethink like this : buddy[dob_month].
This is the rails form helper code,
<%= form_tag({ :action => "create"}, :method => "POST", :id=>"login") do -%>
<h1>Log In</h1>
<fieldset id="inputs">
<%= text_field :username, :placeholder => 'Username', :autofocus=>true%>
<%= password_field_tag :userpass, params[:userpass], placeholder: 'Password', id:'password'%>
</fieldset>
<fieldset id="actions">
<input type="submit" id="submit" value="Log in">
Register
</fieldset>
<% end %>
and this is the html code, what i want,
<form action="/login/create" method="post" id="login" >
<h1>LogIn</h1>
<fieldset id="inputs">
<input id="username" type="text" name ="username" placeholder="Username" autofocus required>
<input id="password" type="password" name="userpass" placeholder="Password" required>
</fieldset>
<fieldset id="actions">
<input type="submit" id="submit" value="Log in" name="apply">
Register
</fieldset>
</form>
However, if i run the rails code, it makes the code like this,
<input id="username_{:placeholder=>"Username", :autofocus=>true}" name="username[{:placeholder=>"Username", :autofocus=>true}]" size="30" type="text">
Somebody knows what the problem is?
You'll want to use text_field_tag in your form, not just text_field:
<%= form_tag({ :action => "create"}, :method => "POST", :id=>"login") do -%>
<h1>Log In</h1>
<fieldset id="inputs">
<%= text_field_tag :username, params[:username], { :placeholder => 'Username', :autofocus => true } %>
<%= password_field_tag :userpass, params[:userpass], placeholder: 'Password', id:'password'%>
</fieldset>
<fieldset id="actions">
<input type="submit" id="submit" value="Log in">
Register
</fieldset>
<% end %>
You should also look into field_set_tag - that may save you some headaches if you're expecting to use the HTML fieldset tag.
I have following structure:
<% if #uploads%>
<br class="clear" />
<br />
<% #uploads.each do |file| %>
<div class="file_information">
<p><%= file.file_name %></p>
<td class="Chip_info">
Info
</td>
<td class="Pic">
Pic
</td>
<td class="Hist">
Hist
</td>
<td class="Hist2">
His2
</td>
<td class="delete">
<button class="btn btn-mini btn-danger" data-url="<%=file.destroy%>">
<i class="icon-trash icon-white"></i>
</button>
</td>
</div>
<%end%>
<%else%>
<br class="clear" />
<br />
<%end%>
The problem is that while running it, it destroys all objects automatically without me clicking on the button. Is it possible somehow to list uploads and only by clicking on the button, the corresponding upload will be deleted (destroy action will be called) and not all of uploads?
Thanks in advance
edit
I use this example for file upload
He uses followint to delete the file:
model:
"delete_url" => upload_path(self)
view:
<td class="delete">
<button class="btn btn-danger" data-url="{%=file.delete_url%}">
<i class="icon-trash icon-white"></i>
<input type="checkbox" name="delete" value="1">
</td>
So I tried
data-url="<%=file.upload_path(self)%>">
but it doesnt work, so I printed file.upload_path(self) from a controller and got this:
/uploads/%23%3CUploadsController:0xaf61b34%3E
You can create a helper that generates a form containing one button:
def button_to_delete_file(file)
form_tag file_path(file), :method => :delete, :style => "display:inline;" do
button_tag :class => "btn btn-mini btn-danger" do
content_tag :i, " ", :class => "icon-trash icon-white"
end
end
end
And use it like this:
<%= button_to_delete_file(file) %>
In the button
<button class="btn btn-mini btn-danger" data-url="<%=file.destroy%>">
your data_url is not an url. When the file is executed, everything inside <% %> is executed, so file.destroy is called.
Replace file.destroy by the corresponding URL (something that looks like file_destroy_path(id)) and it should work.