应该如何在Rails的我的形式看? [英] How should my form look in Rails?
问题描述
我已经命名空间的两种型号。一种模式主题
是父母给发布
模式,发布
分享与标签
模型HABTM关系。没有任何一个模型都验证。
我想收集使用复选框,同时submtting后在一个窗体上的主题和Tag模式设置数据。任何我曾经尝试过一次,但是我碰到<一个href="http://stackoverflow.com/questions/9269142/mass-assign-failing-and-a-hash-with-indifferent-access-rails-3-2">this问题。
我的问题有:
- 我应该如何扩大自己的状态,控制器和模型,以避免在<一个发现的错误href="http://stackoverflow.com/questions/9269142/mass-assign-failing-and-a-hash-with-indifferent-access-rails-3-2">this帖子?
- 请我需要声明我的命名空间,每个redirect_to时?
控制器code 的
的before_filter:check_authentication,只有:[新]
的before_filter:fetch_author,只有:[新,:创建]
的before_filter:fetch_post,只有:[:秀:更新:编辑:灭]
的before_filter:fetch_topic,除了:[:创建]
高清新
@topic = Topic.all
@post = @ user.posts.build
@tag = @ post.tags.build
结束
DEF创建
@post = @ user.posts.build(PARAMS [:帖])
@topic = @ post.topic.build(PARAMS [:帖])
respond_to代码做|格式|
如果@ post.save
的format.html {redirect_to时[@topic,@post]通知:后已成功创建 }
其他
的format.html {渲染动作:新}
结束
结束
结束
DEF更新
如果@ post.update_attributes(PARAMS [:帖])
redirect_to时[@topic,@post]通知:后已成功更新。
其他
渲染:编辑
结束
结束
DEF破坏
@ post.destroy
redirect_to时root_url([@话题,@post]),注意:帖子删除。
结束
私人
高清fetch_author
@user = User.find(会话[:USER_ID])
结束
高清fetch_topic
@topic = Topic.find(PARAMS [:topic_id])
结束
高清fetch_post
@post = @ topic.posts.find(PARAMS [:ID])
结束
这是我的表格的
&LT;%=的form_for([:博客,@topic,@post])也| F | %&GT;
&LT; DIV CLASS =字段&GT;
&LT;%= f.label:标题%&GT;&LT; BR /&GT;
&LT;%= f.text_field:标题%&GT;
&LT; / DIV&GT;
&LT; DIV CLASS =字段&GT;
&LT;%= f.label:含量%&GT;&LT; BR /&GT;
&LT;%= f.text_area:含量,消毒:真,行:15%&GT;
&LT; / DIV&GT;
&LT; DIV CLASS =字段&GT;
&LT;%= f.fields_for(:主题)办|建| %&GT;
&LT;%= build.label:TOPIC_NAME,选择一个主题%&GT;
&LT;%= collection_select(:帖子,:topic_id,Topic.all - [@post]:身份证,:TOPIC_NAME,提示:真正的)%&GT;
&LT;%结束%GT;
&LT; / DIV&GT;
&LT; DIV CLASS =字段&GT;
&LT;%= f.fields_for(:标签)做|建| %&GT;
&其中;%=除非build.object.new_record?
build.check_box('_消灭')+ build.label('_消灭,删除标记)
最终%&GT;
&LT;%= build.label:TAG_NAME,添加标签%&GT;
&其中;%= build.text_field:TAG_NAME%GT;
&LT;%结束%GT;
&LT; / DIV&GT;
&LT; DIV CLASS =行动&GT;
&其中;%= f.submit%GT;
&LT; / DIV&GT;
&LT;%结束%GT;
是的,你需要在每一个redirect_to时定义的命名空间,如果你想这样的嵌套的URL。如果你还没有这个命名空间,你不能有这样的嵌套的URL。
I have namespaced two models. One model Topic
is parent to the Post
model and Post
shares a HABTM relationship with the Tag
model. None of the models have validations.
I would like to gather use a checkbox to set data on the Topic and Tag model while submtting the post in a single form. Any time I have tried however I run into this problem.
Questions I have:
- How should I augment my form, controller and model to avoid the errors found in this post ?
- Do I need to declare my namespace in each redirect_to?
Controller code
before_filter :check_authentication, only: [:new]
before_filter :fetch_author, only: [:new, :create]
before_filter :fetch_post, only: [:show, :update, :edit, :destroy]
before_filter :fetch_topic, except: [:create]
def new
@topic = Topic.all
@post = @user.posts.build
@tag = @post.tags.build
end
def create
@post = @user.posts.build(params[:post])
@topic = @post.topic.build(params[:post])
respond_to do |format|
if @post.save
format.html { redirect_to [@topic, @post], notice: 'Post was successfully created.' }
else
format.html { render action: :new }
end
end
end
def update
if @post.update_attributes(params[:post])
redirect_to [@topic, @post], notice: 'Post was successfully updated.'
else
render :edit
end
end
def destroy
@post.destroy
redirect_to root_url([@topic, @post]), notice: 'Post deleted.'
end
private
def fetch_author
@user = User.find(session[:user_id])
end
def fetch_topic
@topic = Topic.find(params[:topic_id])
end
def fetch_post
@post = @topic.posts.find(params[:id])
end
Here is my form
<%= form_for([:blog, @topic, @post]) do |f| %>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :content %><br />
<%= f.text_area :content, sanitize: true, rows: 15%>
</div>
<div class="field">
<%= f.fields_for(:topic) do |build| %>
<%= build.label :topic_name, "Select a topic" %>
<%= collection_select(:post, :topic_id, Topic.all - [@post], :id, :topic_name, prompt: true) %>
<%end%>
</div>
<div class="field">
<%= f.fields_for(:tags) do |build| %>
<%= unless build.object.new_record?
build.check_box('_destroy') + build.label('_destroy', 'Remove Tag')
end%>
<%= build.label :tag_name, "Add Tag"%>
<%= build.text_field :tag_name %>
<%end%>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Yes you need define your namespace in each redirect_to if you want this type of nested URL. If you have not this namespace you can't have this nested URL.
这篇关于应该如何在Rails的我的形式看?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!