Reputation: 143
I am working on an app for rockhounds that will allow them to track the rocks they have found. I am trying to finish a method that will allow users to update rocks. The method itself works if you go to the '/edit' page where you enter a url, but when trying to reach that page via a link from the show page for the rock I get an error: Param is missing or the value is empty: rock Its coming from line 43 in my rocks controller, inside the method rock_params. The parameters for this request are:
{"_method"=>"patch", "authenticity_token"=>"tiQQjQCMi6lbDPOxQ2X7fNTamWEZ3EAandGQ1Tv4NJKxieVIUnnWcKh3efd9F2LIRZWBYeC5M3RuEr0/FHQnww==", "id"=>"6"}
Here is my controller code
before_action :find_rock, only: [:show, :edit, :update, :destroy]
def index
@rocks = Rock.all
end
def show
@rock = Rock.find_by(id: params[:id])
end
def new
@rock = Rock.new
end
def create
@rock = Rock.new(rock_params)
if @rock.save
redirect_to @rock
else
render :new
end
end
def update
if @rock.update(rock_params)
redirect_to @rock
else
render :edit
end
end
def destroy
@rock = Rock.find(params[:id])
@rock.destroy
redirect_to rocks_path
end
private
def rock_params
params.require(:rock).permit(:id, :nick_name, :category, :minerals, :outcrop)
end
def find_rock
@rock = Rock.find(params[:id])
end
end ```
Here is the view code for the show page:
`<h1> Rock: </h1>
<h2> Rock Name: <%= @rock.nick_name %></h2>
<h2> Rock Type: <%= @rock.category %></h2>
<h2> Major Minerals: <%= @rock.minerals %></h2>
<h2> Outcrop? : <%= @rock.outcrop %></h2>
<%= link_to "Edit this rock", edit_rock_path(@rock), method: :patch %>
<%= link_to "Delete this rock", rock_path(@rock), method: :delete %> `
and the edit page:
```<%= form_for :rock do |f| %>
Name: <%= f.text_field :nick_name %><br>
Category: <%= f.select :category, ['Igneous', 'Sedimentary', 'Metamorphic'] %><br>
Major Minerals <%= f.text_area :minerals %><br>
Outcrop or Boulder? <%= f.select :outcrop, ['Outcrop', 'Boulder'] %><br>
<%= f.submit %>
<% end %> ```
and here are my routes:
```resources :rocks
resources :users
resources :locations
post '/users/new', to:"users#create"
get '/signup', to:"users#new", as: "signup"
post '/rocks/new', to:"rocks#create"
get '/rocks/:id/edit', to:"rocks#edit"
patch '/rocks/:id/edit', to:"rocks#update"
post '/rocks/:id/destroy', to:"rocks#destroy"
root to: "rocks#index"
get '/login', to: "auth#login", as: "login"
post '/login', to: "auth#authenticate"
get '/logout', to: "auth#logout"```
So the question is **How do I fix the empty param error that occurs when clicking the link to edit a rock from the show page for that rock?**
Upvotes: 1
Views: 340
Reputation: 370
@toodles from the rails documentation here, edit paths are of the HTTP verb GET.
You need to change this line <%= link_to "Edit this rock", edit_rock_path(@rock), method: :patch %>
, remove method: :patch
which applies a PUT HTTP verb
It should look like this <%= link_to "Edit this rock", edit_rock_path(@rock) %>
as rails applies the GET verb if the method is not specified.
You'll be able to see this when you inspect your link in a browser
Also, since you defined the resource :rocks
, you don't need to redefine the other rock
routes below it. You can see the generated routes with the rails routes
command in your terminal.
You can also add
an edit
action to your controller, that returns @rock
, which you use in the edit page. This section of the getting started with Rails article shows it clearly.
Upvotes: 1