Rails how to update image? And how to do RESTful routing? - ruby-on-rails

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.

Related

Active Merchant and f.submit button on the same view

I have active merchant the i am using simple form.
Is there a way that i can submit the form and also submit the simple_form data at the same time?
Example:
<%= simple_form_for #billing, url: checkout_shopping_cart_path do |f| %>
<h3>Billing & Registration Details</h3>
<hr>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="inputFirstName" class="control-label">Title:</label>
<div>
<%= f.input :title, collection: [ 'Mr', 'Mrs', 'Ms' ], selected: 'Mr', label: false %>
</div>
</div>
</div>
<%= f.submit 'Submit', class: 'btn btn-default-1' %>
<% end %>
</div>
<% payment_service_for #order.reference_id, 'XXXXXXXXXXXX',
:amount => #order.total*100.round,
:currency => 'SGD',
:credential2 => 'xxxxxxxxxx',
:service => :ipay88 do |service| %>
<% service.customer :first_name => #user.account.first_name,
:last_name => #user.account.last_name,
:email => #user.email,
:phone => #user.phone %>
<% #shopping_cart.shopping_cart_items.each do |shopping| %>
<% service.description shopping.item.name %>
<% end %>
<% service.return_url url_for(:only_path => false,
:controller => 'pay', :action => 'return') %>
<%= submit_tag "Pay", class: 'btn-default-1' %>
<% end %>
The result that i have is having two buttons. How do i make it all at one page?

Rails sending AJAX response with a textarea

I am using the following form:
<%= form_for(#document, :remote => true) do |f| %>
<%= f.label :asset, "Document to Upload" %><br />
<%= f.file_field :asset, :class => "behaves", "data-behave" => "passValueOnChange", "data-recipient" => "#campaign_document_name" %>
<%= f.label :name, :class => 'required' %><br />
<%= f.text_field :name, :required => true %>
<%= f.submit "Upload Document", :class => "button" %>
<% end %>
to submit to the following controller:
def create
#document = CampaignDocument.new(params[:campaign_document])
#campaign.documents << #document
respond_to do |format|
format.js
end
end
which should return the following create.js.erb:
<%= remotipart_response do %>
<% unless #document.errors.any? %>
$('.tablewrapper').html( '<%= escape_javascript(render :partial=>'documents_table') %>' );
alertify.success("File Updloaded");
<% else %>
<% #document.errors.each do |attr, msg|%>
alertify.error("<%= msg %>",10000);
<% end %>
<% end %>
<% end %>
but the response is wrapped in a textare tag. Which is pretty odd:
<textarea data-type="text/javascript" response-code="200">
$('.tablewrapper').html('{code for HTML ommited for brevity}');
alertify.success("File Updloaded");
</textarea>
Note
Other, similar styled AJAX functions on the same installation are not behaving this way.

Nested attributes has_many passing and collecting ids to build model

I have this form:
<% form_tag add_expt_details_samples_path, :method => :post do %>
<% for sample in #samples %>
<% if sample.study_id == #study.id %>
<% fields_for "samples[]", sample do |form| %>
<fieldset>
<legend>Sample Name: <%= sample.name %></legend>
<p><center><%= form.label :sample_title %>
<%= form.text_field :sample_title, :size => 25 %></center></p>
<table>
<tr>
<td>
<%= form.label :taxon_id %>
<%= form.text_field :taxon_id, :size => 15 %>
</td>
<td>
<%= form.label :scientific_name %>
<%= form.text_field :scientific_name, :size => 20 %>
</td>
<td>
<%= form.label :common_name %>
<%= form.text_field :common_name, :size => 15 %>
</td>
</tr>
</table>
<p>
<center>
<%= form.label :sample_descripition %><br \>
<%= form.text_area :description %>
</center>
</p>
</fieldset>
<% end %>
<% end %>
<% end %>
<p><center><%= submit_tag "Next" %></center></p>
<% end %>
I would like to collect the samples[] in add_expt_details action so that I could render a post method view for adding new model records for the samples[] ids.
my action is as follows
def add_expt_details
# Collect the samples that were sent
#expt_samples = Sample.find(params[:samples])
# #expt_samples.each do |samp|
# samp.expts.build
# end
end
and then send them to a create action where the sample details are updated and the expt values can be created.
def create_study
#samples = Sample.update(params[:samples].keys, params[:samples].values).reject { |p| p.errors.empty? }
if #samples.empty?
flash[:notice] = "Samples updated"
redirect_to :action => "add_expt_details", :anchor => "ok"
else
render :action => 'edit_individual'
end
end
is there a way to achieve this, I have used accepted_nested_attributes_for :expts, :dependent => :destroy. But I seem to be wrong some where and I do not want to have a form with many fields.
The error message I get is "Unknown key(s): 3, 6, 12"
All suggestions are appreciated.

rails ignores my redirect

Rails is ignoring my redirect.
Use Case:
user navigates to his account and selects the option to display his subscription status
app lists his current subscription and the option to cancel it
user cancels the subscription
app updates the subscription record and should redirect the user back to the subscriptions action
-> but rails ignores this last step... any ideas what I am doing wrong?
Routes
map.resources :users, ..., :member => { ..., :subscriptions => :get, :subscribe => :post, :unsubscribe => :put}
Controllers:
def subscriptions
#tradesman = User.find_by_id(params[:id])
#subscription = #tradesman.current_subscription || Subscription.new
#all_subscriptions = Subscription.find(:all)
end
def subscribe
#tradesman = User.find_by_id(params[:id])
#subscription = current_user.subscriptions.build(params[:subscription])
#subscription.update_attributes(:started_at => Time.zone.now)
#subscription.save
redirect_to :action => 'subscriptions', :id => #tradesman.id
end
def unsubscribe
#tradesman = User.find_by_id(params[:id])
#subscription = #tradesman.current_subscription
#subscription.update_attributes(:ended_at => Time.zone.now)
#subscription.save
redirect_to :action => 'subscriptions', :id => #tradesman.id
end
View:
<div class = 'wrapper'>
<%= render :partial => "my_account_leftbar" %>
<% form_for #subscription, :url => subscribe_user_path(current_user) do |f| %>
<% #all_subscriptions.each do |subscription| %>
<div class="field">Start: <%= subscription.started_at %></div><br><br>
<% end %>
<% if #subscription.new_record? %>
<div class="field">
<%= f.check_box :subscription_type %>
<div class="actions">
<%= f.submit "Subscribe", :class => "button mr8" %>
</div>
</div>
<% else -%>
<%= f.check_box :subscription_type, :value => #subscription.subscription_type, :disabled => true %>
<% form_for #subscription, :url => unsubscribe_user_path(current_user) do |f| %>
<div class="actions">
<%= f.submit "Unsubscribe", :class => "button mr8" %>
</div>
<% end %>
<% end %>
<% end %>
</div>
Use redirect_to instead of render (last method call in your unsubscribe action).
Found the bug in the view where I nested the 'unsubscribe' form within the 'subscribe' form - what didn't make sense.
I rearranged my view (separated those two forms, one in the 'if' part and the other in the 'else' part. Works fine now:
<div class = 'wrapper'>
<%= render :partial => "my_account_leftbar" %>
<% #all_subscriptions.each do |subscription| %>
<div class="field">Start: <%= subscription.started_at %></div><br><br>
<% end %>
<% if #subscription.new_record? %>
<% form_for #subscription, :url => subscribe_user_path(current_user) do |f| %>
<div class="field">
<%= f.check_box :subscription_type %>
<div class="actions">
<%= f.submit "Subscribe", :class => "button mr8" %>
</div>
</div>
<% end %>
<% else -%>
<% form_for #subscription, :url => unsubscribe_user_path(current_user) do |f| %>
<%= f.check_box :subscription_type, :value => #subscription.subscription_type, :disabled => true %>
<div class="actions">
<%= f.submit "Unsubscribe", :class => "button mr8" %>
</div>
<% end %>
<% end %>
</div>

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