ruby on rails - How to link_to correct show page from feed? -


if user clicks on:

activities/valuations/_create.html.erb

<%= link_to valuation_path(activity) %>   <%= activity.trackable.name %> <% end %> 

he directed to, example, error:

couldn't find valuation 'id'=24

because valuation 7. because line of code trying find valuation show page looking @ id number activity instead of id number valuation.

to fix tried:

<%= link_to valuation_path(@valuation) %>   <%= activity.trackable.name %> <% end %> 

but gives error:

actioncontroller::urlgenerationerror in activities#index

no route matches {:action=>"show", :controller=>"valuations", :id=>nil} missing required keys: [:id]

how rewrite code take user correct valuations show page?

activities_controller

class activitiescontroller < applicationcontroller     def index         @activities = activity.order("created_at desc").paginate(:page => params[:page])     end      def show           redirect_to(:back)     end    def     @activity = activity.find(params[:id])     @activity_like = current_user.activity_likes.build(activity: @activity)     if @activity_like.save       @activity.increment!(:likes)       flash[:success] = 'thanks liking!'     else       flash[:error] = 'two many likes'     end         redirect_to(:back)   end end 

activity.rb

class activity < activerecord::base   self.per_page = 20   has_many :notifications   belongs_to :user   belongs_to :trackable, polymorphic: true    def conceal     trackable.conceal   end    def page_number     (index / per_page.to_f).ceil   end  private    def index     activity.order(created_at: :desc).index self   end end 

activities/index

<% @activities.each |activity| %>     <%= link_to activity.user.name, activity.user %></b>     <%= render "activities/#{activity.trackable_type.underscore}/#{activity.action}", activity: activity %> <% end %> 

it seems want nested resource,

when have activities/valuations/_create.html.erb , means should have nested route

#routes.rb resources :activities   resources :valuations end   

so link should

<%= link_to 'link', activities_valuataions_path(activity, @validation) %> 

if post routes

rake routes

it should easy track down problem


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -