I am trying to make an app in Rails 4.
I have an articles model.
The show page includes a button which is supposed to be a link to an edit (update) method.
I have:
<%= button_to 'Edit', articles_path(article), :method => :update, :class => 'btn btn-large btn-primary' %>
When I try this, I get this error:
NameError in Articles#show Showing //app/views/articles/show.html.erb where line #49 raised:
undefined local variable or method `article' for #<#:0x007fe14dc83030>
Line 49 has the button to edit.
Can anyone see what's wrong?
My show page has:
<div class="col-xs-2">
<%= image_tag @article.image.thumb.url if @article.image? %>
</div>
<div class="col-xs-10">
<div class="articletitle">
<%= @article.title %>
</div>
<div class="commentattributionname">
<%= @article.user.try(:full_name) %>
</div>
<div class="commentattributiontitle">
<%= @article.user.try(:formal_title) %>
</div>
<div class="commentattributiondate">
<%= @article.created_at.try(:strftime, '%e %B %Y') %>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-9">
<div class="intpol3" style="text-align: left; margin-left:60px; padding-top:50px">
<%= safe_join(@article.body.split("\r\n"), "<br />".html_safe) %>
</div>
</div>
<div class="col-sm-3">
<!-- placeholder for tags -->
</div>
</div>
<!-- if @article.user.full_name.present? -->
<!-- <div class="indexsubtext"> @article.user.full_name </div> -->
<!-- end -->
<div class="row">
<div class="col-xs-8">
</div>
<div class="col-xs-4">
<div class="formminor" style="margin-bottom:5%">
<% if policy(@article).update? %>
<%= button_to 'Edit', articles_path(article), :method => :update, :class => 'btn btn-large btn-primary' %>
<% end %>
|
<%= link_to 'More from the blog', articles_path %>
</div>
</div>
</div>
My articles controller has:
class ArticlesController < ApplicationController
before_action :set_article, only: [:show, :edit, :update, :destroy]
def set_article
@article = Article.find(params[:id])
authorize @article
end
My routes have:
resources :articles do
collection do
get 'search'
end
resources :comments, module: :articles
end
If I change it to:
<%= button_to 'Edit', articles_path(article), :method => :update, :class => 'btn btn-large btn-primary' %>
I get this error:
ActionController::ParameterMissing in ArticlesController#create
param is missing or the value is empty: article
My controller has:
def create
# before_action :authenticate_user!
# authorize @article
@article = current_user.articles.new(article_params)
respond_to do |format|
if @article.save
format.html { redirect_to(@article) }
format.json { render :show, status: :created, location: @article }
else
format.html { render :new }
format.json { render json: @article.errors, status: :unprocessable_entity }
end
end
end
def article_params
params.require(:article).permit(:body, :title, :image, :tag_list,
comment_attributes: [:opinion])
end
TAKING TAGLIA'S SUGGESTION
I get this error:
ActionController::ParameterMissing in ArticlesController#create
param is missing or the value is empty: article
This means you've not defined article
anywhere in your controller. You need to define the variable article = Article.find(params: [:id])
or similar in your Articles show action, that way you should be able to find the article you need.
If you have used resources :articles
to generate the routes for your articles, rails provide you a specific helper to access the edit action. This should work:
<%= button_to 'Edit', edit_article_path(@article), :class => 'btn btn-large btn-primary', method: :get %>
Your code had two problems:
articles_path
is a helper which generates the path to the index action, and does not require any arguments;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.