ruby - How to retrieve id sent from a link_to in rails -
i trying to edit form through reveal-modal. have multiple posts.
when click link 'edit' reveal div , display form within repopulated information. however, don't know how pass post id form know post edit.
if seek render partial form error:
undefined method `model_name' post::activerecord_relation:class
all info on controller view called dashboard
controllers/dashboards_controller.rb
class dashboardscontroller < applicationcontroller def index @profile = profile.find(current_user) @profileall = profile.all @post = post.all end def show end def edit @profile =profile.find(current_user) @post = @profile.post.find(params[:id]) end end controllers/posts_controller.rb
class postscontroller < applicationcontroller def create @profile = profile.find(current_user) @post = @profile.post.create(post_params) redirect_to dashboards_path(current_user) end def destroy @post =profile.find(params[:profile_id]) @post = profile.post.find(params[:id]) @post.destroy redirect_to dashboards_path(current_user) end def show @profile =profile.find(current_user) @post = @profile.post.find(params[:id]) end def edit @profile =profile.find(current_user) @post = @profile.post.find(params[:id]) end def update @post = profile.post.find(params[:id]) if @post.update(post_params) redirect_to dashboards_path(current_user) else render 'edit' end end private def post_params params.require(:post).permit(:title, :content) end end posts/_form2.html.erb
<%= form_for @post |f| %> <% if @post.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@post.errors.count, "error") %> prohibited post beingness saved:</h2> <ul> <% @post.errors.full_messages.each |msg| %> <li><%= msg %></li> <% end %> </ul> </div> <% end %> <p> <%= f.label :title %><br> <%= f.text_field :title %> </p> <p> <%= f.label :content %><br> <%= f.text_area :content %> </p> <p> <%= f.submit %> </p> <% end %> routes.rb
rails.application.routes.draw 'homepages/index' resources 'dashboards' resources 'posts' end #get 'users/sign_in' resources 'profiles' resources 'posts' end devise_for :users, :controller => {:registrations => "users/registrations"} devise_scope :user root :to => 'devise/sessions#new' end end dashboards/index.html.erb
<% @post.each |post| %> <div class="panel"> <%= @profileall.find(post.profile_id).name %> <hr> <h5><%= post.title %></h5> <p><%= post.content %></p> <%# link_to "edit", edit_dashboard_post_path(current_user,[post.profile, post]) %> <%= link_to "edit", {id: @post},'data-reveal-id' => 'edit-post' %> </div> <% end %> <div id="edit-post" class="reveal-modal" data-reveal> <%= render 'posts/form2' %> <a class="close-reveal-modal">×</a> </div>
well, not work way. first, have in controller @post = post.all , within _form2.html.erb want render form collection. should be:
# index.html.erb <div id="edit-post" class="reveal-modal" data-reveal> <%= render :partial => 'posts/form2', collection: @posts %> <a class="close-reveal-modal">×</a> </div> #dashboard controller: def index #... @posts = post.all end _form2.html.erb: every @post object instance has replaced form2. read more here: http://api.rubyonrails.org/classes/actionview/partialrenderer.html rendering collection.
however, consider case when have hundreds of posts. then, each post, partial rendered. inefficient instead, consider asynchronous request load 1 post user requested.
ruby-on-rails ruby
No comments:
Post a Comment