Popup with edit action not working in rails 3 - ruby-on-rails

I have popup which is opened using:
<%= link_to user.username, "/users/"+user.id.to_s+"/edit", :method => :post, :target=> "_blank" %>
edit.html.erb file is:
<div id="dvUserMgmt" class="popup">
<%= form_for(:user, :url => { :controller => 'users', :action => 'update'}) do |f| %>
<table>
<tr>
<td class="labelfield">*Name</td>
<td class="textfield">
<input type="text" name="tb_realname" value=<%= #realname %> />
</td>
</tr>
<tr>
<td class="buttons" colspan="3">
<%= submit_tag 'Update', :url => { :controller => 'users', :action => 'update'}, :class => 'popup_closebox' %>
<%= tag :input, :type => 'button', :value => 'Cancel', :class => 'popup_closebox' %>
</td>
</tr>
</table>
<% end %>
</div>
Upon clicking update button, the action update is not getting executed.
Can anyone point where is the issue?

Lots of code is not written the Rails way, let's correct it and see if it changes your result.
1) correct your line, but explain wh you have a post method for an edit?
<%= link_to user.username, edit_user_path(user), :method => :post, :target=> "_blank" %>
2) Rails knows your entry already exists, I guess #user is defined
<%= form_for #user do |f| %>
3) use the form helpers:
<%= f.submit 'Update', :class => 'popup_closebox' %>
4) Provide your params to help debugging.

Related

Rails form submit operate like a link_to

So ultimately what I'm trying to do is get the form to be split across two different columns with the status to be in one column and the save button in another column next to the link_to OR have the form's submit operate like a link_to. The form automatically applies some CSS that's causing the issue of splitting the form.
<tbody>
<% #training_resource.spud_users.each do |training| %>
<tr>
<td><%= training.full_name %></td>
<% utr = training.user_training_resources.where(training_resource: #training_resource).first %>
<td class="utr-form">
<%= tb_form_for [:admin, utr], url: admin_update_utr_path(utr), :remote => true, :html => {:id=>'form_id'}, :data => {:errors => :inline, :success => admin_training_resources_path} do |f| %>
<%= f.select :status, UserTrainingResource.statuses.map {|k,v| [k.humanize, k]}, selected: utr.status %>
</td>
<td class="table-actions">
<%= f.tb_save_buttons('', admin_update_utr_path(utr)) %>
<% end %>
<%= link_to 'submit', admin_update_utr_path(utr), :onclick => "$('#form_id').submit()" %>
<%= link_to 'Delete', admin_destroy_utr_path(utr), :method => :delete, :data => {:confirm => 'Are you sure you want to delete this?'}, :class => 'btn btn-danger btn-sm' %>
</td>
</tr>
<% end %>
</tbody>
So what I'm trying to figure out is if there is a way to change the form save button to be a link_to. Right now I have it here under link_to 'submit'. It however does not operate like the tb_save_button as it doesn't redirect to the correct location or save.
You could handle this in the controller.
(example)
def create
if utr.save
redirect_to admin_update_utr_path(utr)
else
# render new form or display the previous view
end
end

passing data to a rails partial

Edit: Solution Worked Perfectly.
dom_id
I am trying to pass data to a partial and use it to change roles of a user. Right now it will only change the roles of one user. When it is rendered it only displays and acts on the roles of User.first
Where it is rendered. i pass the user in locals. as per this post I used the second answer because the first one didnt work and gave me errors.
<%= render :partial => 'roles', :locals => { :user => user } %>
Below is the partial _role.html.erb
<div id="roleModal" class="reveal-modal">
<%= semantic_form_for user, :url => user_path(user), :html => {:method => :put } do |f| %>
<h3>Change Role</h3>
<div class="reveal-modal-body">
<%= f.input :roles, :as => :check_boxes, :input_html => { :selected => user.role_ids } %>
</div>
<a class="close-reveal-modal">×</a>
<%= f.action :submit, :as => :button %>
<% end %>
<% #users.each do |user| %>
<tr>
<td><%= link_to user.name, user %></td>
<td><%= user.email %></td>
<td><%= user.created_at.to_date %></td>
<td><%= user.roles.first.name.titleize unless user.roles.first.nil? %></td>
<td>
Change role
<%= render :partial => 'roles', :locals => { :user => user } %>
</td>
</tr>
<% end %>
You had the original question about passing locals to a partial correct. The issue was using non-unique pairs of IDs for revealing your hidden forms. So the reveal links always revealed the first occurence of the div with that ID, thus always changing the role on the first user.
Try updating these two lines to use the Rails dom_id() helper to create unique matching pairs of data-reveal-id to id as per the Foundation reveal modal docs:
<a href="roles#<%= user.id %>" data-reveal-id="<%= dom_id(user) %>"
class="button" type="button">Change role</a>
<div id="<%= dom_id(user) %>" class="reveal-modal">

observe_field in a rails 2.2.2 app is not saving checkbox value

I have the following form in a rails app which is not working:
<% form_remote_for :team do |f| %>
<tr>
<% 1.upto(12) do |i| %>
<td>
<%= f.check_box #mo[i] %>
<%= observe_field 'team_' + #mo[i],
:method => :put,
:with => "$(#mo[i] + '=') + $('team_'+#mo[i]).value" %>
</td>
<% end %>
</tr>
<% end %>
#mo[i] is the name of the field in the database (e.g. jan, feb, mar,... dcm). The code would look like this if i were to write them all out:
<% form_remote_for :team do |f| %>
<tr>
<td>
<%= f.check_box :jan %>
<%= observe_field 'team_jan',
:method => :put,
:with => "'jan=' + $('team_jan').value"
</td>
...
</tr>
<% end %>
Thanks for your help.
Your :with statements has some syntax problems, and you are also not interpolating your ruby variables correctly. Try this:
:with => "'#{#mo[i]}=' + $('team_#{#mo[i]}').value"
I derived this by taking your intended output 'jan=' + $('team_jan').value and replacing all instances of jan with #{#mo[i]}, which is the way to interpolate your variable that contains the month name.
Also, you probably need to add a :url option to tell where your ajax call should be sent. Something like this:
<%= observe_field 'team_jan',
:url => team_path(#team),
:method => :put,
:with => "'#{#mo[i]}=' + $('team_#{#mo[i]}').value" %>
I am guessing here that your desired endpoint is team_path(#team), but replace it if it is not.

Rails how to update image? And how to do RESTful routing?

I am trying to update my photographer model. But how problems with Rails routing and paperclip wont replace image.
When I submit my form the url is: http://localhost:3000/admin/photographers/save.75 which gives an error. Couldn't find Photographer without an ID
And my image is not updated.
My form:
<%= simple_form_for #photographer, :url => save_admin_photographers_path(#photographer), :html => {:multipart => true, :method => :post} do |f| %>
<%= javascript_include_tag "tiny_mce/tiny_mce" %>
<%= javascript_include_tag "init_my_tiny_mce" %>
<%= f.input :name, :label => 'Name' %>
<%= f.text_area :text, :label => 'Text', :size => '12x12' %>
<%= f.file_field :image, :label => 'Image' %>
<% if #photographer %>
<% if #photographer.image %>
<p class="ok">
<label for="dd">image ok</label>
<%= image_tag("http://s3-eu-west-1.amazonaws.com/kjacobsen/photographer/image/#{#photographer.id}/#{#photographer["image"]}", :size => "39x22") %>
</p>
<p>
<label for="sdd"> </label>
<%= link_to("remove", {:action => "remove_image", :id => #photographer.id}, {:confirm => "Are your sure?"}) %>
</p>
<% else %>
<p class="current">
<label for="dd"></label>
No image uploaded for movie
</p>
<% end %>
<br />
<% end %>
<br />
<%= f.file_field :flv, :label => 'Upload FLV' %>
<br />
<% if #photographer %>
<% if #photographer.flv %>
<p class="ok">
<label for="dd">flv: <%= #photographer["flv"] %></label>
<%= link_to("remove", {:action => "remove_flv", :id => #photographer.id}, {:confirm => "Are your sure?"}) %>
</p>
<% else %>
<p class="current">
No flv uploaded
<br /><br />
</p>
<% end %>
<% end %>
<br />
<%= f.file_field :quicktime, :label => 'Upload Quicktime' %>
<br />
<% if #photographer %>
<% if #photographer.quicktime %>
<p class="ok">
<label for="dd">quicktime: <%= #photographer["quicktime"] %></label>
<%= link_to("remove", {:action => "remove_quicktime", :id => #photographer.id}, {:confirm => "Are your sure?"}) %>
</p>
<% else %>
<p class="current">
<label for="dd"></label>
No quicktime uploaded
<br />
</p>
<% end %>
<% end %>
<%= f.button :submit, :value => 'Create movie' %>
<% end %>
My update controller:
def save
#photographer = Photographer.find(params[:id])
#photographer.update_attributes(params[:photographer])
if !#photographer.save
flash[:notice] = " "
render_action 'edit'
else
flash[:notice] = "update ok"
redirect_to :action => 'edit', :id => #photographer.id
end
end
My routes:
namespace :admin do
resources :photographers do
collection do
post :save
end
end
end
What you're doing is basically an update action. The update route is automatically created when you do resources :photographers, you can verify this by typing rake routes in the terminal.
You should rename your controller action from save to update and remove the custom route:
namespace :admin do
resources :photographers
end
Then use the update route in your form:
:url => admin_photographer_path(#photographer)
You should also change your html method, the update action uses PUT:
:method => :put
Hope this helps.

Rails form submit problem having 2 forms

I have that problem it is always the same form that gets submitted.
The update_limits action gets called on the update order submit button. Which should trigger the action update_order.
Here is my view:
<h2>Movies</h2>
<h3>List movies</h3>
<%= form_tag(:action => 'update_limits' ,:id => params[:id]) %>
<%= link_to 'create new movie', {:action => 'create',:id => params[:id]}, {:class => 'margin-left'} %>
<div class="spacer"> </div>
Number of movies in reel:
<span class="c1">
<% rr = 1..6 %>
<%= select("limits", "reel_limit", rr) %>
</span>
Number of movies in archive:
<span class="c1">
<% rr = 0..12 %>
<%= select("limits", "archive_limit", rr) %>
</span>
<%= submit_tag %>
<div class="spacer"> </div>
<%= form_tag(:controller => 'admin/photographers', :action => 'update_order' ,:id => params[:id]) %>
<ul id='movielist'>
<span class="header">name</span>
<%
n = 0
while n < #items.length
%>
<li itemID='<%=#items[n].id%>' <%= reel_color_class(n, #limits) %>>
<% if #items[n]["image"] %>
<%= image_tag("/photographer/image/#{#items[n].id}/#{#items[n]["image"]}", :size => "36x20" ) %>
<% end %>
<%=#items[n].name.force_encoding("UTF-8") %>
<span class='col2'>
<%= link_to 'edit', {:action => "edit", :id => #items[n].id} %>
<%= link_to("remove", {:action => "remove", :id => #items[n].id },
{:confirm => "Are your sure?"}) %>
</span>
</li>
<%
n = n + 1
end
%>
</ul>
<input type="hidden" name="neworder" id="neworder" value="" />
<input name="commit" type="submit" value="update order" onclick="neworder.value=(junkdrawer.inspectListOrderNew('movielist'))" />
<div class="spacer"> </div>
The form_tag method takes a block, and you are not giving it one. You should be doing something like this:
<%= form_tag(:action => 'update_limits' ,:id => params[:id]) do %>
# form goes here
<% end %>
Or even better, if this is acting on a real object, using a form_for tag:
<%= form_for(#object) do |f| %>
# form_goes here
<% end %>
For more information, please read the Getting Started guide for Rails.

Resources