Rails 3 - link for delete item - ruby-on-rails-3

I have list of images and for every image I have link for deleting. That link looks this:
<%= link_to 'delete image', {:controller => 'shops', :action => 'delimg', :imgid => u.id}, :confirm => 'Really?' %>
def logo
puts params[:imgid]
...
end
And I am getting an error Couldn't find Shop with ID=logo and app/controllers/shops_controller.rb:17:in `show' - I tried to add puts 'IN SHOW and it really looks, that after click on that link is called method show. I have no idea, how it is possible...
Could someone help me, please, where is the problem?

This is probably how I would have done it:
#routes.rb
resources :shops do
delete :delimg, :on => :member
end
By adding that, there will be a defined route to the delimg action mapped to the delete method. And that makes it possible to do the following in the view:
<%= link_to 'delete image', delimg_shop_path(u.id), :method => :delete %>
delimg_shop_path is a path helper that exists because of what was added in the routes.rb

you are displaying params[:ingid] in the logo method but in the link to action u have specified delimg??
modiefy your link to as
<%= link_to 'delete image', {:controller => 'shops', :action => 'logo', :imgid => u.id}, :confirm => 'Really?' %>
then it will work

Related

unable to update role and stripe plan rails

I'm attempting to update my stripe subscription via and edit page but it doesn't seem to like whatever i throw at it.
Here is my routes.rb
get 'subscribe' => 'subscribe#stepone'
get 'subscribe/sliding-scale' => 'subscribe#steptwo'
get 'subscribe/sliding-scale/subscriber' => 'subscribe#subscriber'
get 'subscribe/sliding-scale/supporter' => 'subscribe#supporter'
get 'subscribe/sliding-scale/sustainer' => 'subscribe#sustainer'
post 'subscribe/sliding-scale/:type' => 'subscribe#createSubscription'
get 'subscribe/edit' => 'subscribe#edit', :as => :edit_subscription
match '/subscribe/edit', to: 'subscribe#deleteCard', via: :delete
match '/subscribe/edit', to: 'subscribe#updateSubscription', via: :post
post 'subscribe/edit/changeSubscription' => 'subscribe#changeSubscription', :as => :change_subscription
My subscription controller:
def changeSubscription
customer = Stripe::Customer.retrieve(#user.stripeCustomerId)
#plan = params[:plan]
#user.update_subscription(:plan => #plan, :prorate => true)
current_role = #user.roles.first.name
#user.remove_role current_role
current_user.add_role params[#plan]
end
and lastly my edit view:
<h2>Change My Subscription</h2>
<h3>Your current plan is: <%= current_user.roles.first.name %>
<%= select_tag #plan, options_for_select([['Subscriber'], ['Sustainer'], ['Supporter']]) %>
<%= link_to "Update", change_subscription_path, :confirm => "You sure?", :plan => #plan %>
I'm attempting to send the new plan to the controller to update both the local role and the plan but it seems not to be able to find the proper route to do so. Not sure what i'm doing wrong in this regard.
Thanks for your help!
You need to perform a POST request to change_subscription_path, but you're requesting the page with a simple link_to so it's a GET request.
Add :method => :post in your link_to options:
<%= link_to "Update", change_subscription_path, :confirm => "You sure?", :method => :post, :plan => #plan %>
Alternatively, you can:
use a form instead of a link and submit it with the POST method
change the method for the changeSubscription action from POST to GET in routes.rb

Rails: No route matches [PUT] "/blog/2"

I am creating blog application in rails. I have a common form for creating and updating blog.
This is view of edit and new.html.erb
<%= render :partial => "form"%>
This is view of _form.html.erb blog:
<%= form_for #blog do |f| %>
<%= f.text_field :title, :placeholder => "Title" %><br>
<%= f.cktext_area :article, :placeholder => "Content", :ckeditor => {:toolbar => "MyToolbar"} %>
<%= f.submit %>
<% end %>
My blog is creating successfully but I am getting error on update action. This is my edit and update action in blog controller:
def edit
#blog = Blog.find_by_slug(params[:id])
end
def update
#blog = Blog.find(params[:id]) || not_found
#blog.update_attributes(params[:blog])
redirect_to "/blogs/#{#blog.slug}"
end
When I open form from edit view, and click on update button, it throws error:
No route matches [PUT] "/blog/2"
My routes.rb is:
resources :blogs
get 'blog', to: 'blogs#index'
get '/blog/:id', to: 'blogs#show', as: 'blog'
I am not getting where it is going wrong. I tried to add "url: blogs_path" in form_for, it removes the error but doesn't save the edit changes.
Can anybody help me where I am going wrong here?
Thank you.
Okay. I dont understand why you want to go against conventions. Anyway, using form_for resource would automatically generate action URL as a PUT to /resources/:id if its an update operation.
So to override this you need to do two things.
update your routes to support this:
Add this line to your routes file:
put 'blog/:id' => 'blogs#update', :as => 'update_blog'
It is important that you put this line above your 'resources :blogs` call.
2 . specify the URL to which the form should submit:
You will need to create the form tag like this:
<%= form_for #blog, :url => update_blog_path(#blog) do |f| %>
Try this and let us know.

how to pass id to controller in rails 3?

I have used link_to to call controller action.
<%= link_to "Inactive", {:controller => :leave_details, :action => :deactivate}, {:method => :post } %>.
but my requirement is to pass id also to controller action but when i try to pass id(:id=>leave_detail.id) it is not showing in params. my params look like
{"_method"=>"post", "authenticity_token"=>"3vfyGQ5V6GQXRt2aQt+DOT0b3eGgP7B401uclnIGLUU=", "action"=>"deactivate", "controller"=>"leave_details"}.
In route file i have added
resources :leave_details do
post :deactivate, :on => :collection
end
can anyone tell me what is going wrong in this code or what i am missing.
thnks

Delete link goes to show method

I'm using Rails 3, i have a link_to with method :delete, but when i click this link it goes to the show action instead the destroy one.
my question is how to solve this problem?
thanks in advance.
code: <%= link_to 'Delete', list_path(#list.id), :method => :delete, :confirm => 'Are you sure?' %>
You don't have the jquery and jquery_ujs libraries included by your application.js file, or you're not even including your application.js file in app/views/layouts/application.html.erb. This is typically done with this line:
<%= javascript_include_tag "application" %>
Hi you can try this:
application.html.erb:
<%= javascript_include_tag "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js", "jquery.rails.js" %>
and your link code should be like this:
<%= link_to "<i class='icon-trash'></i> Delete".html_safe, item, :confirm => "Are you sure you want to delete item: " + item.name + "?" ,:method => :delete%>
and your controller should have like this:
def destroy
#item = Item.find(params[:id])
if #item.destroy
redirect_to items_path, :notice => "Successfully deleted an item."
else
redirect_to items_path, :notice => "Failed to delete an item."
end
end

Rendering the Devise edit Password Form

I'm trying to render the Devise edit password form within another view because I don't want to duplicate the edit pw logic.
I've tried the following (after generating the Devise views):
<%= render 'devise/passwords/edit' %>
<%= render 'devise/passwords/form' %>
And a number of other variations on render that all seem to give me the same error:
"ActionView::MissingTemplate in foo#foo
Missing partial devise/passwords/edit..."
This variation:
<%= render :file => 'devise/passwords/edit.html.erb' %>
Gave me some hope but the following error:
"undefined local variable or method `resource' for #<#:0x47ef0e0>"
around this line:
<%= form_for(resource, :as => resource_name, :url => password_path(resource_name), :html => { :method => :put }) do |f| %>
That makes me think I'm close (as that is code from the form that I want) but shouldn't that template be using the correct logic from the hidden Devise controller? Or do I need to do something in the routes file to get this to work?
Am I way off?
Try this:
<%= render :template => 'devise/passwords/edit',
:locals => {
:resource => my_user_model_variable,
:resource_name => my_user_model_name } %>
Where:
my_user_model_variable could be current_user
my_user_model_name could be "User"