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.
Related
I have this helper in my user index... all good.
<%= link_to user.name, edit_user_path(user) %>
What i'd like to do is have that helper buried/hidden in pencil glyph that links to the opening of a modal which in turn is in the edit_user_path.
I can get everything to work if I do this
<td class= "centre" >
<p data-placement="top" data-toggle="tooltip" title="Edit">
<button class="btn btn-primary btn-xs" data-title="edit"
data-toggle="modal" data-target="#edit"
<%= link_to user.name, edit_user_path(user) %> >
<span class="glyphicon glyphicon-pencil"></span>
</button>
</p>
</td>
The link is physically in the button but also visible on the page...How should I be doing this
Hours later...EDIT...
I can now get everything working with the username hidden and only the glyph showing (which is correct) but by removing the user.name I now render a blank form no user content. I should say at this point that both signup and edit are sharing a form partial
this almost works, but renders a blank form for signup not edit... and its not very rails
<td class= "centre" >
<p data-placement="top" data-toggle="tooltip" title="Edit">
<button class="btn btn-primary btn-xs" data-title="edit" data-toggle="modal" data-target="#edit" >
<span class="glyphicon glyphicon-pencil"></span>
</button>
</p>
</td>
I have a rails app that has links included in bootstrap buttons.
<td>
<button class="btn btn-default"><span class="glyphicon glyphicon-eye-open"></span>
<%= link_to 'Details', user_story %>
</button>
</td>
The HTML shows up properly, and the link is assigned correctly in the anchor tags:
<td><button class="btn btn-default"><span class="glyphicon glyphicon-eye-open"></span> Details
</button>
</td>
It works in chrome but not in Firefox or IE. However when I remove the bootstrap styling, the link_to function does work...
How can I keep bootstrap styling on my pages and keep cross browser compatibility? Or is there a way to force rails to assign the anchor tags around the button?for example: <td><button class="btn btn-default"><span class="glyphicon glyphicon-eye-open"></span> Details</button></td>
Update:
Now in firefox but not in IE it works using:
<% link_to user_story do %>
<button class="btn btn-default"><span class="glyphicon glyphicon-eye-open"></span> Details</button>
<% end %>
It is not valid HTML to have a link inside of a button. It is easy to make links look like buttons, though. Try something like:
<td>
<%= link_to user_story, class: 'btn btn-default' do %>
<span class="glyphicon glyphicon-eye-open"></span> Details
<% end %>
</td>
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?
I have a "search" page that have some controls and below is the search page code:
<%= form_for :search, :url => { :method => :get, :action => :search } do |f| %>
<table>
<tr>
<td align="center" style="vertical-align:top;">
<h2 style="color:Black; font-size: x-large;">Specs</h2>
<table>
<tr>
<td align="center">
<input type="text" name="tf_Zip" Style="text-align: left;" BackColor="#e5e5e5" Width="180px" ForeColor="Gray" Font-Size="Large">
</td>
</tr>
</table>
<table>
<tr>
<td>
<div class="button">
<input type="submit" name="search" value="Search" class="buttonSearch">
</div>
</td>
</tr>
</table>
</td>
<td align="center" style="vertical-align:top;">
<h2 style="color:Black; font-size: x-large;">
Result
</h2>
<% #user_zip.each do |uzr_zip| %>
<h1><%= uzr_zip.First_Name %></h1>
<% end %>
<table id="searchResult" width="100%" runat="server">
<tr>
<td bgcolor="#CCDBE0">
Image:
</td>
<td bgcolor="#CCDBE0">
<%= f.label(:zip, "Mentor") %>
</td>
</tr>
</table>
</td>
</tr>
</table>
<% end %>
And when I am trying to get the textbox value into the controllers page like below
def search
#students=Students.all
#blah = params[:search][:tf_Zip]
end
render 'search'
end
Then it gave me an error below, at this line #blah = params[:search][:tf_Zip]
undefined method `[]' for nil:NilClass
Kindle help me. Thanks
I think your params[:search] is nil?
so for this you can use
#blah = params[:tf_Zip]
or change your input field like this
<%= f.text_field :tf_Zip %>
or you can use like this
<input type="text" name="search[tf_Zip]" Style="text-align: left;"
BackColor="#e5e5e5" Width="180px" ForeColor="Gray" Font-Size="Large">
Look at your log: you will see what is coming through in params, then you'll see why params[:search][:tf_Zip] doesn't work.
The error is telling you, effectively, that params[:search] is nil, and that you can't call [tf_Zip] on nil.
Your problem is this line:
<input type="text" name="tf_Zip" Style="text-align: left;" BackColor="#e5e5e5" Width="180px" ForeColor="Gray" Font-Size="Large">
It will populate params[:tf_Zip] because it's name is "tf_Zip". If you want it to populate params[:search][:tf_Zip] then you should set the name attribute to search[tf_Zip].
What would be nicer though is to use the rails form field helpers. I don't know why you have so much raw html inside a form_for.
<input type="text" name="tf_Zip" Style="text-align: left;" BackColor="#e5e5e5" Width="180px" ForeColor="Gray" Font-Size="Large">
can be replaced with
<%= f.text_field :tf_Zip %>
which will populate params[:search][:tf_Zip]
For the other attributes (Style etc) you should set these with css. Rails will probably put a class on the field automatically which you can use to do this. The "style" (note lowercase) attribute can be used instead but it's clumsy as it doesn't allow you to restyle the field (and more generally, your site) with css.
Have you checked to see if params[:search] is nil? If it is, then trying to pull [:tf_Zip] will cause that error.
Usually you'd submit the form from one controller action and handle the result in another action.
And your statement end looks misplaced.
Here is my code, i have wasted lot of time on this but still did not get the answer.Form ended in the same row where it started and because of this submit method is not adding the checkbox values in database.please help?
< table>
<% pos = 0%>
<% #accounts.each do |myacc| %>
<% if #accounts.length == pos +1 %>
<tr class="last">
<% else%>
<tr>
<% end %> <!-- end of if tag -->
<td class="position"><%= pos += 1%>.</td>
<td ><%= myacc.name %>< /td>
< % form_for(:account,:html =>{:method =>:put,:id =>"editAccountForm_"+pos.to_s,:multipart => true}) do|f|%>
<td>
<% if myacc.place_matching == "1" %>
<input type="checkbox" id=place_matching value=1 checked="checked" />
<% else %>
<input type="checkbox" id=place_matching value=0 />
<% end %> <!-- end of if tag -->
</td>
<td>
<% if myacc.data_pulling == "1" %>
<input type="checkbox" id="data_pulling" value=1 checked="checked" />
<% else %>
<input type="checkbox" id="data_pulling" value=0 />
<% end %> <!-- end of if tag -->
</td>
<td>
<a class="gray-button gray-whitebg" href="#" onclick="getElementById('editAccountForm_<%= pos%>').submit();return false;" />ADD</a>
</td>
<% end %> <!-- end of form tag -->
</tr>
<% end %>
</table>
In rails 3 you always have to use <%= %> form, also for blocks.
So write
<%= form_for ...
I see you use the form_for, but then not use f at all. Why not use f.check_box ? See documentation.
Or use form_tag and then use check_box_tag. See documentation.
You should be using more of the helpers rails gives you.
Please paste your complete form..
please take you form out from your table..
hope this will help