I'm making one simple rails application with simple forms for crud operations and one rails api that will have models and do all crud operations with data from rails application. In application i'm using ActiveResource. Currently i'm having some problems with update...Everything else works only when i update it breaks and i don't know why, plz help.
api/postcontroler
class API::PostsController < ApplicationController
before_action :set_post, only: [:show, :update, :destroy]
# GET /posts
# GET /posts.json
def index
@posts = Post.all
render json: @posts
end
# GET /posts/1
# GET /posts/1.json
def show
render json: @post
end
# POST /posts
# POST /posts.json
def create
@post = Post.new(:title => params[:title], :content => params[:content])
if @post.save
render json: @post, status: :created, location: @post
else
render json: @post.errors, status: :unprocessable_entity
end
end
# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
if @post.update_attributes(post_params)
render json: @post
else
render json: @post.errors, status: :unprocessable_entity
end
end
# DELETE /posts/1
# DELETE /posts/1.json
def destroy
@post.destroy
head :no_content
end
private
def set_post
@post = Post.find(params[:id])
end
def post_params
params.require(:post).permit(:title, :id, :content)
end
end
application/postcontroler
class PostsController < ApplicationController
before_action :set_post, only: [:show, :update, :edit, :destroy]
def index
@posts = Post.all
end
def new
@post = Post.new(:title => "", :content => "")
end
def create
@post = Post.new(post_params)
if @post.save
redirect_to post_path(@post)
else
render "new"
end
end
def show
@comments = Comment.find(:all, :params => {:post_id => params[:id]})
end
def edit
end
def update
@post.title = params[:title]
@post.content = params[:content]
if @post.save
redirect_to post_path(@post)
else
render "edit"
end
end
def destroy
@post.destroy
redirect_to :action => 'index'
end
private
def post_params
params.require("post").permit(:title, :content)
end
def set_post
@post = Post.find(params[:id])
end
end
application/edit.html.erb
<%= form_for(@post, :url=>{:action=>'update'}) do |f| %>
<%= f.label :title %><br>
<%= f.text_field :title %><br>
<%= f.label :content %><br>
<%= f.text_area :content %><br>
<%= f.submit %>
<% end %>
application routes
resources :posts do
resources :comments
end
api routes
namespace :api, :defaults => {:format => :json} do
resources :posts, except: [:new, :edit] do
resources :comments, except: [:new, :edit]
end
end
application active resource post model
class Post < ActiveResource::Base
self.site = "http://localhost:3001/api"
end
I start application at port 3000 and api at 3001,when i try to update this happens
on application application
and this on api api
What's the problem?
You have the formats mixed up for your two controllers.
api/postcontroller is expecting non-nested parameters, and application/postcontroller is expecting nested parameters, but you are passing them in the opposite way. So the api version fails, because the require(:post)
fails, and the application version fails, because the data you are looking for is in `params[:post][:title], not params[:title].
Try swapping the bodies of the two update methods (with the appropriate modification, you don't want the application serving Json etc).